![](/img/trans.png)
[英]Changing the value of a Form Submit Button to say submitted after form submits
[英]allocate text value submitted by a form to a variable after clicking submit button
我想通过单击表单的提交按钮将提交的文本值存储在变量中,以便可以将该变量用于进一步查询数据库。
我的代码:
<?
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>
//在实际情况下,表单具有带单选按钮的元素,其中包含来自数据库查询的值,我想使用表单中的选定项来处理同一页面中的另一个数据库查询...
提前致谢
提交的表单数据自动分配给变量(在您的情况下为$ _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>
尝试这个 -
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.