0

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

我的代码:

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

提前致谢

4

2 回答 2

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>
于 2013-05-14T10:56:11.453 回答
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>
于 2013-05-14T10:56:30.660 回答