cregy Posted December 7, 2007 Share Posted December 7, 2007 Hi I have been trying to search (in Google) for answers for ages but cannot hit upon the correct search terms so I ended up here. I have just joined and I am trying to learn php. My first question surrounds if/else statements. I want to do a multiple choice form. Basically if you answer this then go here and answer this, then go here and answer this and then you get told something. So, I ask: 1. Are you buying or selling. 2a. If the answer is buying then you get asked registered or unregistered. 2b. If the answer is selling then you get asked registered or unregistered as well. 3. You then get asked price of the property 4. Finally you get told fees will be £xxxx depending on decisions. So whilst the statements are similar I am trying to work out how to get from this: <?php if( $first == 'selling') { echo 'You have selected selling<br /> <p>Now select whether the property is registed or unregistered please<br /> <p>Options: <select name="second"> <option value = "registered">Registered</option> <option value = "unregistered">Unregistered</option> </select> </p>'; } else { echo 'You are buying'; } echo '<p>Fees processed at '; echo date('H:i, jS F'); echo '</p>'; echo '<p>Your fees are as follows: </p>'; ?> As you can see I have entered the form details and when you select selling you get asked the registered or unregistered question but how do I now process the next step please? Many thanks Rich Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 7, 2007 Share Posted December 7, 2007 First thoughts are having a set of scripts, one for each question, something like this: question1.php question2.php question3.php ...and so-on. You'd need some sort of validation between questions to prevent someone jumping to a question by typing the URL directly into the address bar. How are you storing the data - using a MySQL database? Where for example in question 2 you ask a question dependent on the answer of question 1 you could use an if/else statement similar to above to present the relevant question. Quote Link to comment Share on other sites More sharing options...
cregy Posted December 7, 2007 Author Share Posted December 7, 2007 Hi Thanks for the reply. I haven't worked out how to get this attached to mysql yet - that will come. The problem I am having is understanding how to use multiple statements. So how do I get the answer to question 1 so that it goes to the correct next question please? Thanks Rich Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 7, 2007 Share Posted December 7, 2007 Something like this for question1.php: <form action="question2.php" method="post"> Are you buying or selling? <select name="buysell"><option value="1">Buying</option><option value="2">Selling</option></select><br /> <input type="submit" name="subq1" value="Submit" /> </form> That's the HTML part to display the question in a basic form. It makes a drop-down box and a submit button. Notice the action in the form points to question2.php Now in question2.php your code could be something like this: <?php if ($_POST['subq1']) { $buysell=$_POST['buysell']; if ($buysell==1) { //DISPLAY THE QUESTION 2A } else { //DISPLAY THE QUESTION 2B } } ?> Obviously there's no error checking there as it's the basics. The two comments would be where you'd display the alternate questions by either ending PHP and going back to HTML or using PHPs echo to display the form. Quote Link to comment 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.