提交表单后将变量传递给php表单

时间:2011-08-08 12:43:44

标签: php forms variables

我想在不使用会话的情况下将变量从php页面传递给另一个。

这里是来自

的SearchCustomer.php页面
<form action="controllers/Customer.controller.php" method="post">
<label for="cellPhoneNo">cell phone number</label>
    <input type="text" name="cellPhoneNo" class="textField"/>
    <span id="cellphonePrefix"></span>

    <label for="telephone">telephone </label>
    <input type="text" name="telephone" class="textField"/>

    <input type="submit" name="searchCustomer" value="بحث"/>

在Customer.controller.php中我搜索客户并返回$result,我想将Customer.controller.php中定义的变量传递给SearchCustomer.php页面,该页面提交来自而不使用会话。

4 个答案:

答案 0 :(得分:4)

您可以使用隐藏的输入控件来实现此目的。

在Customer.controller.php中:

echo "<input type='hidden' name='result' value='{$result}'>";

在SearchCustomer.php中:

$passedResult = $_POST['result'];

更新:问题确实发生了一些变化,使用类似的重定向:

header("Location: http://yoursite.com/SearchCustomer.php?result={$result}");

答案 1 :(得分:2)

你可以用某种方式利用隐藏的字段!

例如:

<input type="hidden" name="multiPageValue" 
value="<?php echo $_POST['multiPageValue'];?>"/>

您可以通过提交表单继续在您要导航的所有连续页面上使用此剪辑!

答案 2 :(得分:2)

用户提交表单后,您的服务器将处理发送给它的表单数据。 PHP接受并在名为$ _POST的数组中为您解析它。

PHP $_POST

由于您未在表单中定义操作,因此_POST数组一旦提交,就可以与表单一起使用。在页面顶部进行简单的测试:

if(isset($_POST['cellPhoneNo'])) {
 echo "Thank you for your phone numbers!";
}

答案 3 :(得分:2)

据我了解您的问题:您想在Customer.controller.php中执行搜索并立即在SearchCustomer.php中显示结果,对吗?

好吧,闻起来像AJAX:)

试试这个(使用jQuery):

<强> SearchCustomer.php

<form id="search_form" action="controllers/Customer.controller.php" method="post">
//...
</form>

<div id="output">
</div>

<script>
$('#search_form').submit(function() {
  $.post($(this).attr('action'), $(this).serialize(), function(data) {
    for(var i = 0; i < data.length; i++) {
      $('#output').append('field one: ' + data[i].field1 + '<br />');
    }
  }, 'json');
  return false;
});
</script>

<强> Customer.controller.php

<?php

$cellPhoneNo = $_POST['cellPhoneNo'];
//....

//perform the search, 
//fetch the assoc-array, e.g. $results 
echo json_encode($results);

代码未经测试,但您明白了;)