我想通过在变量中单击表单的提交按钮来存储提交的文本值,以便我可以使用该变量进一步查询数据库。
我的代码:
<?
if($submit)
{
mysql_connect("localhost:3036","root","root");//database connection
mysql_select_db("sync");
$order = "INSERT INTO country (id,country) VALUES ('44','$submit')";
$result = mysql_query($order);
if($result){
echo("<br>Input data is succeed");
} else{
echo("<br>Input data is fail");
}
}
?>
<html>
<title>form sumit</title>
<body>
<form method="post" action="">
<input type="text" name="id" value="<?=$submit;?>"/>
<input type="Submit" name="submit" value="Submit">
</form>
</body>
</html>
//在实际情况中,表单包含带有单选按钮的元素,其中包含来自DB QUERY的值, 我想使用表单中的选定项目来处理同一页面中的另一个数据库查询...
先谢谢
答案 0 :(得分:0)
提交的表单数据会自动分配给变量(在您的情况下为$ _POST)。如果您想要长期存储,请考虑使用$ _SESSION变量,否则在脚本终止时将丢弃提交的数据。
请澄清你的问题,因为我不太清楚你想在这里实现什么。
在正常的工作流程中,您首先要检查您的表单是否已被处理(查看$ _POST是否有任何值得处理的数据),然后在数据库中查询表单所需的任何数据,然后呈现实际表单。
正如所承诺的,这是一个动手实践的样本:
<?php
if ($_POST['ajax']) {
// This is a very trivial way of detecting ajax, but we don't need anything more complex here.
$data = workYourSQLMagicHere(); //data should be filled with the new select's html code
print_r(json_encode($data));
die(); // Ajax done, stop here.
}
/* Your current form generation magic here. */
?>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
// This should probably go into a separate JS file.
$('#select1').change( function() {
var url = ''; //Here we're accessing the page which originates the script. If you have a separate script, use that url here. Local only, single-origin policy does not allow cross-domain calls.
var opts = { ajax: true };
$.post(url, opts, function(data) {
$('#select2').replaceWith( $.parseJSON(data) ); //Replace the second select box with return results
});
});
</script>
<select id="select1"><?=$stuff;?></select>
<select id="select2"><?=$more_stuff;?></select>
答案 1 :(得分:0)
试试这个 -
<?php
$submit = $_POST['id'];
if($submit)
{
//your code is here
echo $submit;
}
?>
<html>
<title>form sumit</title>
<body>
<form method="post" action="">
<input type="text" name="id" value="<?php echo $submit; ?>"/>
<input type="Submit" name="submit" value="Submit">
</form>
</body>
</html>