简体   繁体   中英

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

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. Thanks for the help.

You have to redirect the first form to a new page then look at the $_POST variable.

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

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>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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