Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中,如何使用同一表单中一个提交按钮与另一个提交按钮之间的变量_Php_Html_Function_Variables - Fatal编程技术网

在php中,如何使用同一表单中一个提交按钮与另一个提交按钮之间的变量

在php中,如何使用同一表单中一个提交按钮与另一个提交按钮之间的变量,php,html,function,variables,Php,Html,Function,Variables,在我的php代码中,有一个表单内部有多个submit按钮,第一个表单的变量在单击submit按钮后分配值,第二个表单的变量类似于$\u POST[]。在这里,我想在第二次提交中使用第一次提交的值 我的代码如下: <?php global $name; //BLOCK1 if($_POST['formSubmit'] == "Register Yourself") { global $name;

在我的php代码中,有一个表单内部有多个submit按钮,第一个表单的变量在单击submit按钮后分配值,第二个表单的变量类似于$\u POST[]。在这里,我想在第二次提交中使用第一次提交的值

我的代码如下:

<?php
global $name;

          //BLOCK1
    if($_POST['formSubmit'] == "Register Yourself") 
        {
        global $name;           
        $name = $_POST['name'];
        echo $name;
    };
         // BLOCK 2

    if($_POST['formSubmit1'] == "Register Yourself1") 
        {
        global $name;           
        $id = $_POST['id'];
        echo $id;
            echo $name;   // I want to print "$name" here

    };

?>
<html>
<title>form</title>
<body>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <b> <label for='name'>Name: </label> </b>
    <input type="text" name="name" maxlength="50" value="<?=$name;?>" />
<input type="submit" name="formSubmit" value="Register Yourself" />
<br/><br/>
<b> <label for='id'> id: </label> </b>
    <input type="text" name="id" maxlength="50" value="<?=$id;?>" />
<input type="submit" name="formSubmit1" value="Register Yourself1" />
</form>
</body>
</html>

形式

我解释了正确的答案,但是如果您是PHP新手,您可能不理解该注释

您引用的链接询问如何在函数之间共享变量。上面示例中的代码未使用函数;它使用“如果”条件

如果您的代码不起作用,则与您对$\u POST数组的使用更相关。当用户提交表单时,单击哪个按钮并不重要;$\u POST数组将包含该表单中各种输入的所有数据。因此,您可以通过以下方式获得更大的成功:

// let's set up the empty variables for clarity's sake
$name = '';
$id = '';

// now check if the $_POST array has stuff in it
if(!empty($_POST)) 
{
    if (!empty($_POST['name'])) {
        $name = $_POST['name'];
        echo '<p>' . $name . '</p>';
    }

    if (!empty($_POST['name'])) {
        $id = $_POST['id'];
        echo '<p>' . $id . '</p>';
    }
};

<form action="" method="post">
    <b> <label for='name'>Name: </label> </b>
    <input type="text" name="name" maxlength="50" value="<?=$name;?>" />
    <br/><br/>
    <b><label for='id'> id: </label> </b>
    <input type="text" name="id" maxlength="50" value="<?=$id;?>" />
    <br /><br />
    <input type="submit" name="formSubmit" value="Register Yourself" />
</form>
//为了清晰起见,让我们设置空变量
$name='';
$id='';
//现在检查$\u POST数组中是否有内容
如果(!空($\u POST))
{
如果(!空($_POST['name'])){
$name=$_POST['name'];
回显“”.$name.“

”; } 如果(!空($_POST['name'])){ $id=$_POST['id']; 回显“”.$id.“

”; } }; 姓名:
我在想,如果你没有得到你想要的值,下面的值是不正确的

     if($_POST['formSubmit'] == "Register Yourself") 

 if($_POST['formSubmit1'] == "Register Yourself1") 
因此,我建议您尝试以下代码:

 if($_POST['formSubmit'] == "Register Yourself") 
        {

  echo $_POST['name'];
    }else{
        echo 'formSubmit = '.$_POST['formSubmit'];
    }
         // BLOCK 2

    if($_POST['formSubmit1'] == "Register Yourself1") 
        {
  echo $_POST['id'].'<Br/>';
  echo $_POST['name'].'<Br/>';


    }else{
        echo 'formSubmit1 = '.$_POST['formSubmit'];
if($\u POST['formSubmit']=“注册自己”)
{
echo$_POST['name'];
}否则{
回显“formSubmit=”。$\u POST['formSubmit'];
}
//第2区
如果($_POST['formSubmit1']==“注册您自己1”)
{
echo$_POST['id'].
; echo$_POST['name'].
; }否则{ 回显“formSubmit1=”.$\u POST['formSubmit'];

如果您发布的内容与值不匹配,它会告诉您,您不必在内部和外部声明它,如果测试不是功能,它们就是。您根本不需要使用全局设置。我不明白您的意思……请澄清您需要阅读以下内容:嗨,bethadele的sol为我工作了……谢谢您的建议……是的bethadele,我非常感谢您的建议ew到PHP编程,我理解你的解决方案…但我想使用一个DB查询,使用$id和$name两个值,所以请你解释我该怎么做…提前感谢…。嗨,bethadele,我在我的原始程序中有一些疑问,这些都从你的解决方案中清除了…现在可以工作了:)thnks