简体   繁体   English

php使用第一个表单返回数据创建第二个表单

[英]php Create second form using the first form return data

The problem I'm working on is this: I have a form which ask user to type in a name. 我正在处理的问题是:我有一个表格,要求用户输入名称。 Then i search this name in my database, the database will return several addresses. 然后我在数据库中搜索此名称,该数据库将返回几个地址。 Then i need to use those addresses to generate second radio button form. 然后,我需要使用这些地址来生成第二个单选按钮形式。 User will choose one address, and then they get redirected to that address.php 用户将选择一个地址,然后将他们重定向到该地址。

The entire process is like when you shop on amazon, after you log in with your username, it will ask you to select your shipping address and once shipping address is select, i want to redirect user to their address page. 整个过程就像在亚马逊上购物时,使用用户名登录后,将要求您选择送货地址,一旦选择送货地址,我便要将用户重定向到他们的地址页面。

what i current have is following 我目前拥有的是

//error checking----------------------------------
if (empty($_POST['name'])===true) {
echo"filling in your name"
}
else{
//get value from database------------------------------        
    $query="get address";
    $sql_result=mysql_query($query);
    if(mysql_num_rows($sql_result)==0){
    echo "no result found";
    }
    else {
    //second form--------------------------------------------------------
    while ($row = @mysql_fetch_assoc($sql_result)){
                    echo "<form name=\"select\" method=\"post\">";
            echo '<input type="radio" name="name" value="'.$row['name'].'"> '.$row['name'];
            echo '<br>';                
        }
        echo "<input type=\"submit\" name=\"choice\" value=\"Submit\" />";          
    }
    }
}

//first form-----------------------------------------------
<form action="" method="post">
<ul>
    <li>name: <br> <input type="text" name="name">
    </li>       
    <li><input type="submit" value="Find it">
    </li>
</ul>
</form>

I have no idea how to proceed from here. 我不知道如何从这里开始。 Where shall i put my first form so that only first form is displayed when the page is loaded. 我应该在哪里放置我的第一个表格,以便在加载页面时仅显示第一个表格。 Only second form is displayed when first form is submitted. 提交第一份表格时,仅显示第二份表格。 I tried to use $_GET like 我试图像这样使用$ _GET

if(isset($_GET['success'])&& empty($_GET['success'])){
form2;
redirect(location:address.php);
}
else{
get name and search address;
get address;
redirect(location:samepage?success)
form1;}

But this didn't work since address is generated in else statement, i can not access it from if statement. 但这是行不通的,因为在else语句中生成了地址,我无法从if语句访问它。 Thanks for the help. 谢谢您的帮助。

You have to redirect the first form to a new page then look at the $_POST variable. 您必须将第一个表单重定向到新页面,然后查看$ _POST变量。

You can do it this way : 您可以这样操作:

firstpage 第一页

<form action="secondpage.php" method="post">
<ul>
    <li>name: <br> <input type="text" name="name">
    </li>       
    <li><input type="submit" value="Find it">
    </li>
</ul>
</form>

secondpage.php secondpage.php

if(!isset($_POST['name']))
{
    redirect(location:firstpage.php);
}
else
{
    // MySql request to check username

    $resultArray // you result as an array

    echo '<select>';
    foreach ($resultArray as $value){
        echo '<option value="'+$value+'">'+$value+'</option> 
    }
    echo '</select>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM