-1

我在一个文件(index.php)中有两个 HTML 表单和两个 PHP 块。例如,我希望第二个 php 脚本属于第二种形式。我不知道,该怎么做。我给动作属性写了什么?

这是我的代码:

<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="name"> <br>
<input type="submit">
</form>

<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="age"> <br>
<input type="submit">
</form>

<?php
echo $_POST ["name"];
?>

<?php
echo $_POST ["age"]; 
?>
4

3 回答 3

0

希望对你有帮助

第一种形式,

    <form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?> ">
        <input type="text" name="name"> <br>
        <input type="submit" name='submit' >
    </form>

<?php

    if(isset($_POST['submit'])){
            echo $_POST ["name"];
    }
?>

第二种形式

    <form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?>">
        <input type="text" name="age"> <br>
        <input type="submit" name='submitsecond' > // name submitsecond indicates as second form
    </form>

<?php

    if(isset($_POST['submitsecond'])){
            echo $_POST ["age"];
    }
?>  
于 2013-11-09T14:09:01.017 回答
0

您可以使用隐藏的输入字段来区分这两个脚本。而且您必须回显/打印脚本名称 ( $_SERVER['PHP_SELF']),不需要 htmlspecialchars ...

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
    <input type="hidden" name="form" value="name_form" />
    <input type="text" name="name"> <br>
    <input type="submit">
</form>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
    <input type="hidden" name="form" value="age_form" />
    <input type="text" name="age"> <br>
    <input type="submit">
</form>

<?php if($_POST['form'] == 'name_form'): ?>
    The name form is submitted.<br>
    Name: <?php echo $_POST['name']; ?>
<?php endif; ?>

<?php if($_POST['form'] == 'age_form'): ?>
    The age form is submitted.<br>
    Age: <?php echo $_POST['age']; ?>
<?php endif; ?>
于 2013-11-09T14:12:34.127 回答
-1
 <form method="post" action="<?php echo htmlspecialchars($_SERVER ["PHP_SELF"]); ?>">
<input type="text" name="name"> <br>
<input type="submit" name="name_sub">
</form>

    <?php
    if(isset($_POST ["name_sub"])) // check if name form is submit
    echo $_POST ["name"]; 
    ?>
于 2013-11-09T14:06:06.943 回答