[英]allocate text value submitted by a form to a variable after clicking submit button
I want to store the text value submitted by clicking the submit button of a form, in a variable, so that I can use that variable for further querying the DB. 我想通过单击表单的提交按钮将提交的文本值存储在变量中,以便可以将该变量用于进一步查询数据库。
My Code: 我的代码:
<?
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>
//In real case, the form has elements with radio button containing values from a DB QUERY, I wanted to use the selected item from the form to process another DB query in the same page... //在实际情况下,表单具有带单选按钮的元素,其中包含来自数据库查询的值,我想使用表单中的选定项来处理同一页面中的另一个数据库查询...
Thanks in Advance 提前致谢
Submitted form data automatically gets allocated to a variable ($_POST, in your case). 提交的表单数据自动分配给变量(在您的情况下为$ _POST)。 If you want longer-term storage, consider using the $_SESSION variable, otherwise the submitted data is discarded upon script termination. 如果要长期存储,请考虑使用$ _SESSION变量,否则在脚本终止时将丢弃提交的数据。
Please clarify your question, as I'm not quite sure what you are trying to achieve here. 请澄清您的问题,因为我不太确定您要在此处实现什么目标。
In a normal workflow, you would first check if your form has already been processed (see if $_POST has any data worth processing), then query the database for whatever data you need for your form, then render the actual form. 在正常的工作流程中,您将首先检查表单是否已被处理(请查看$ _POST是否有任何值得处理的数据),然后在数据库中查询表单所需的任何数据,然后呈现实际的表单。
As promised, here's a hands-on sample: 如所承诺的,这是一个动手示例:
<?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>
Try this - 尝试这个 -
<?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.