Php 单击“提交”按钮后,将表单提交的文本值分配给变量

Php 单击“提交”按钮后,将表单提交的文本值分配给变量,php,javascript,html,Php,Javascript,Html,我希望通过单击表单的submit按钮将提交的文本值存储在变量中,以便我可以使用该变量进一步查询数据库 我的代码: <? if($submit) { mysql_connect("localhost:3036","root","root");//database connection mysql_select_db("sync"); $order = "INSERT INTO country (id,country) VALUES

我希望通过单击表单的submit按钮将提交的文本值存储在变量中,以便我可以使用该变量进一步查询数据库

我的代码:

<?
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查询的值, 我想使用表单中的选定项来处理同一页面中的另一个DB查询


提前感谢

在您的情况下,提交的表单数据会自动分配给变量$\u POST。如果你想长期存储,考虑使用$$会话变量,否则提交的数据在脚本终止时被丢弃。 请澄清你的问题,因为我不太确定你在这里想要实现什么

在正常的工作流中,您首先要检查表单是否已处理,查看$\u 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>

你在哪里办理表格邮寄。。我看不见。此外,您正在使用短标签。。确保您的php.ini已针对它进行了配置我为其提供的代码是一个示例代码,但实际上,我在填充基于第一个列表的选择的第二个下拉列表时被绊住了。在第一个列表中,两个列表都显示来自某个DB查询的结果,而不点击提交按钮。我必须找到部门列表,并从数据库中仅显示所选部门提供的课程。。。。由于我没有做到这一点,我想做同样的事情来存储submit按钮的值,然后在上面应用一些db查询。。。。有两种方法可以做到这一点:要么使用来自$\u POST的响应数据一步一步地构建表单,为每个步骤提交表单,要么使用JavaScript和AJAX动态填充第二个下拉列表。你需要使用一个补充脚本。第二个选项是我想要使用的,我想请你给我发送一些相同的示例教程和示例。。。提前谢谢。我会看看今晚我是否能为你起草一个更具个人特色的样本,同时,我建议你查阅关于这个主题的W3C资源。使用这个优秀的AJAX教程,您可能能够自己解决问题。很抱歉耽搁了,现在有点急着详细说明。明天晚上有更可行的例子,对吗?