多个表单发布到1个文档中的多个PHP脚本

时间:2015-04-09 15:35:09

标签: php

我试图发布表单:

<form role="form" method="post">
    <select name="inputStore">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
    <button type="submit">Show items</button>
</form>

到PHP脚本中,其中生成另一个表单。现在我试图将PHP表单发布到当前文档中的另一个PHP脚本。

示例:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>

<body>

<form>First form posts to PHP</form>
<?php First PHP which generates another Form ?>
<form>Form which got generated from the first PHP post. This form should POST to the second PHP</form>
<?php Second PHP ?>
</body>

</html>

我怎样才能使这个工作? PHP脚本必须保持其位置。不能将这两个包装在一个PHP标签

1 个答案:

答案 0 :(得分:0)

这是你想做的事情

这里我只是将值发送给另一个,只是将其重新调整为父文件。

index.php (就在前面)

   <script src="http://code.jquery.com/jquery-1.7.2.js"></script>
<form role="form" method="post">
    <select name="inputStore" onchange="getval(this);">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
</form>
<div id='result'>
</div>

<script type="text/javascript">
    function getval(sel) {
    var datas =sel.value;
    console.log(datas);
       $.ajax({
        type: "POST",
        url : "response.php",
         data: { 
        'selected': datas, 
    },
        success : function(data)
        {
        console.log(data);
        $("#result").html(data);
        }
    },"json");

    }
</script>

response.php 这将处理您的请求

<?php
$value = $_POST['selected'];
echo 'This is sent from server '.$value.' - End';
?>

更新:

(由于提问者希望同时看到2个输入的输出)

这是前文件:

<script src="http://code.jquery.com/jquery-1.7.2.js"></script>
<form role="form" method="post">
    <select name="inputStore" id="inputStore" >
        <option value="stud">Male Hottie</option>
        <option value="babe">Female Hottie</option>
    </select>
    <select name="inputStoretwo" id="inputStoretwo">
        <option value="20037336">Artizans</option>
                                                        <option value="20080134">Bwabies!</option>
                                                        <option value="20097365">Crave Mart</option>
                                                        <option value="20099998">David &amp; Goliath</option>
                                                        <option value="20098166">Domo</option>
                                                        <option value="20101926">Emily the Strange</option>
                                                        <option value="20098167">Garfield</option>
                                                        <option value="26">Jetsetter</option>
                                                        <option value="3">Like Dat</option>
                                                        <option value="21">Paris Hilton</option>
                                                        <option value="19">Rocawear</option>
                                                        <option value="10">ShoeBuy</option>
                                                        <option value="20100198">Skelanimals</option>
                                                        <option value="20096121">SW&amp;TH</option>
                                                        <option value="1">The Castle</option>
                                                        <option value="4">The Lair</option>
                                                        <option value="923">The Mix</option>
                                                        <option value="20098121">The Powerpuff Girls</option>
                                                        <option value="5">The Surf Shop</option>
                                                        <option value="20076231">Tie The Knot</option>
                                                        <option value="20099224">Tokidoki</option>
                                                        <option value="2">University Club</option>
                                                        <option value="6">Z Avenue</option>
                                                        <option value="20099506">Z's Greetings</option>
                                                        <option value="20">Snoop Dog</option>
    </select>
</form>
<button>Pass the value</button>

<div id='result'>
</div>

<script type="text/javascript">

$("button").click(function(){
   var first = $("#inputStore option:selected").text();
   var second = $("#inputStoretwo option:selected").text();
    $.ajax({
        type: "POST",
        url : "response.php",
         data: { 
        'firstvalue': first, 
        'secondvalue': second, 
    },
        success : function(data)
        {
        console.log(data);
        $("#result").html(data);
        }
    },"json");
});
</script>

这是响应文件:

<?php
$first = $_POST['firstvalue'];
$second = $_POST['secondvalue'];
echo 'This is first value : '.$first.'<br> and this is the second value : '.$second;
?>