StroiX Posted April 6, 2008 Share Posted April 6, 2008 When a user visits my page, I would like them to have 2 choices in the form of radio buttons: 1) Personal Information 2) Work Information http://www.drychalk.com/_/step1.html In the next step, depending on the choice they made in the previous step, I would like to ask them for the information. However, regardless of whichever choice they select, I always want to ask them for the "Other Information", please take a look: http://www.drychalk.com/_/step2.html My question is that how do I use PHP to display the appropriate form fields and hide the others so that I am asking them only about the choice they made in the previous step (and for the "Other Information"). Do I embed all of the form in step2.html in a php file that I use for the "action" attribute in the form of step1.html? or should I do something else... Ultimately I would like to save the information submitted by the user in a MySQL database. http://www.drychalk.com/_/step2.html It would be very much appreciated is someone could provide a working example of this, but as always, just your advice is also much appreciated!!! =) Quote Link to comment https://forums.phpfreaks.com/topic/99780-need-advice-please-read/ Share on other sites More sharing options...
pocobueno1388 Posted April 6, 2008 Share Posted April 6, 2008 Try this. this is the code that goes on step 2. <?php $group1 = $_POST['group1']; if ($group1 == "personal"){ print<<<HERE <form id="form1" name="form1" method="post" action=""> <p><strong>Personal Information</strong></p> <p>Full Name: <label> <input type="text" name="name" id="name" /> </label> </p> <p>Cell Phone: <label> <input type="text" name="cellphone" id="cellphone" /> </label> </p> HERE; } else if ($group1 == "work") { print<<<HERE <form id="form1" name="form1" method="post" action=""> <p><strong> Work Information</strong></p> <p>Company Name: <label> <input type="text" name="company" id="company" /> </label> </p> <p>Work Phone: <label> <input type="text" name="workphone" id="workphone" /> </label> </p> HERE; } ?> <hr /> <p><strong>Other Information</strong></p> <p>Your message:</p> <p> <label> <textarea name="message" id="message" cols="45" rows="5"></textarea> </label> </p> <p> </p> <p> <label> <input type="submit" name="submit" id="submit" value="Submit" /> </label> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/99780-need-advice-please-read/#findComment-510325 Share on other sites More sharing options...
StroiX Posted April 6, 2008 Author Share Posted April 6, 2008 Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/99780-need-advice-please-read/#findComment-510425 Share on other sites More sharing options...
StroiX Posted April 6, 2008 Author Share Posted April 6, 2008 Quick question for you: you have "print<<<HERE" and "HERE;" in the code... what am I supposed to be replacing this with? Also... as of now I have a "process.php" which the form uses to insert the submitted information into a MySQL database. Should I keep it as it is; separate, or should I include it within the step2.php code. With it being separate right now, I am running into some issues of not having all of the fields filled in since some of them are not being shown. It is giving me this error when running locally: Notice: Undefined index: company in /Web Directory/ur/process.php on line 14 Notice: Undefined index: workphone in /Web Directory/ur/process.php on line 14 1 record added Please refer to the following code from "step2.php" and "process.php". Step2.php: <?php $group1 = $_POST['group1']; if ($group1 == "personal"){ print<<<HERE <form id="form1" name="form1" method="post" action="process.php"> <p><strong>Personal Information</strong></p> <p>Full Name: <label> <input type="text" name="name" id="name" /> </label> </p> <p>Cell Phone: <label> <input type="text" name="cellphone" id="cellphone" /> </label> </p> HERE; } else if ($group1 == "work") { print<<<HERE <form id="form1" name="form1" method="post" action="process.php"> <p><strong> Work Information</strong></p> <p>Company Name: <label> <input type="text" name="company" id="company" /> </label> </p> <p>Work Phone: <label> <input type="text" name="workphone" id="workphone" /> </label> </p> HERE; } ?> <hr /> <p><strong>Other Information</strong></p> <p>Your message:</p> <p> <label> <textarea name="message" id="message" cols="45" rows="5"></textarea> </label> </p> <p> </p> <p> <label> <input type="submit" name="submit" id="submit" value="Submit" /> </label> </p> </form> process.php <?php // 1. Create a database connection $connection = mysql_connect("x", "x", "x"); if (!$connection) { die("Database connection failed: " . mysql_error()); } // 2. Select a database to use $db_select = mysql_select_db('ur', $connection); mysql_query("INSERT INTO personal (name, cellphone) VALUES ('$_POST[name]', '$_POST[cellphone]')"); mysql_query("INSERT INTO work (company, workphone) VALUES ('$_POST[company]', '$_POST[workphone]')"); mysql_query("INSERT INTO message (message) VALUES ('$_POST[message]')"); echo "1 record added"; mysql_close($connection) ?> http://www.drychalk.com/_/ur/step1.html Thank you once again. Quote Link to comment https://forums.phpfreaks.com/topic/99780-need-advice-please-read/#findComment-510426 Share on other sites More sharing options...
mwasif Posted April 6, 2008 Share Posted April 6, 2008 This is called heredoc syntax to print the string. You can also use echo instead. Quote Link to comment https://forums.phpfreaks.com/topic/99780-need-advice-please-read/#findComment-510555 Share on other sites More sharing options...
StroiX Posted April 6, 2008 Author Share Posted April 6, 2008 Got it. Thank you! Can someone please also take a look my "process.php" and see if this really is the only best option to insert the data. And please also read the previous post by me. Quote Link to comment https://forums.phpfreaks.com/topic/99780-need-advice-please-read/#findComment-510768 Share on other sites More sharing options...
pocobueno1388 Posted April 7, 2008 Share Posted April 7, 2008 The error is probably because it's trying to do BOTH inserts when really you only need one. So you need to check which form was submitted, then add the information to the necessary table. <?php // 1. Create a database connection $connection = mysql_connect("x", "x", "x"); if (!$connection) { die("Database connection failed: " . mysql_error()); } // 2. Select a database to use $db_select = mysql_select_db('ur', $connection); if (isset($_POST['name']) && isset($_POST['cellphone'])){ mysql_query("INSERT INTO personal (name, cellphone) VALUES ('{$_POST['name']}', '{$_POST['cellphone']}')"); } else if (isset($_POST['company']) && isset($_POST['workphone'])){ mysql_query("INSERT INTO work (company, workphone) VALUES ('{$_POST['company']}', '{$_POST['workphone']}')"); } mysql_query("INSERT INTO message (message) VALUES ('{$_POST['message']}')"); echo "1 record added"; mysql_close($connection) ?> I would also suggest using mysql_real_escape_string() on all variables before using them in a query, especially POST variables. Quote Link to comment https://forums.phpfreaks.com/topic/99780-need-advice-please-read/#findComment-510868 Share on other sites More sharing options...
StroiX Posted April 7, 2008 Author Share Posted April 7, 2008 Thank you for post! I will give this a shot and will post any problems I run into. Thank you once again! =) Quote Link to comment https://forums.phpfreaks.com/topic/99780-need-advice-please-read/#findComment-510955 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.