prathameshkakade Posted June 30, 2013 Share Posted June 30, 2013 Hello everyone.Well I was just trying out a PHP code and I encountered following errors. 1.The variable $a here is not beig displayed even though I declared it. Here is the code. <html> <head> <title> Choice </title> </head> <body> <?php if(isset($_POST['posted'])) { $a = $_POST['company']; if($a=='maruti') { echo "Select your favourite car from below.<br/>"; echo "<form method='POST' action='qwerty.php'>"; echo "<input type='hidden' name='posted1'>"; echo "<input type='radio' name='maruti1' value='swift'/>Swift<br/>"; echo "<input type='radio' name='maruti1' value='wagonr'/>WagonR<br>"; echo "<input type='radio' name='maruti1' value='maruti800'/>Maruti 800<br/>"; echo "<input type='submit' value='submit'/>"; echo "<hr/>"; } else if($a=='mahindra') { echo "Select your favourite car from below.<br/>"; echo "<form method='POST' action='qwerty.php'>"; echo "<input type='hidden' name='posted1' value='true'/>"; echo "<input type='radio' name='mahindra1' value='scorpio'/>Scorpio<br/>"; echo "<input type='radio' name='mahindra1' value='xylo'/>Xylo<br/>"; echo "<input type='radio' name='mahindra1' value='bolero'/>Bolero<br/>"; echo "<input type='submit' value='submit'/>"; echo "</form>"; echo "<hr/>"; } else if($a=='toyota') { echo "Select your favourite car from below.<br/>"; echo "<form method='POST' action='qwerty.php'>"; echo "<input type='hidden' name='posted1' value='true'/>"; echo "<input type='radio' name='toyota1' value='corolla'/>Corolla<br/>"; echo "<input type='radio' name='toyota1' value='innova'/>Innova<br/>"; echo "<input type='radio' name='toyota1' value='etios'/>Etios<br/>"; echo "<input type='submit' value='submit'/>"; echo "</form>"; echo "<hr/>"; } } else { if(isset($_POST['posted1'])) { echo "Do you own it?<br>"; echo "<form method='POST' action='qwerty.php'>"; echo "<input type='hidden' name='posted2' value='true'/>"; echo "<input type='radio' name='own' value='yes'/>Yes<br/>"; echo "<input type='radio' name='own' value='no'/>No<br/>"; echo "<input type='submit' value='submit'/>"; echo "</form>"; } else { if(isset($_POST['posted2'])) { echo "So you like $a "; if(($_POST['own'])=='yes') { echo "and you own it<hr>."; } else { echo "but you don't own it.<hr>"; } } ?> <form method="POST" action="qwerty.php"> <input type="hidden" name="posted" value="true"/> Which is your favourite Car manufacturing company? <br> <br> <input type="radio" name="company" value="maruti">Maruti Suzuki</input> <br> <input type="radio" name="company" value="mahindra">mahindra</input> <br> <input type="radio" name="company" value="toyota">toyota</input> <br> <input type="submit" value="submit"> </form> <?php } } ?> </body> </html> Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/279707-variable-error/ Share on other sites More sharing options...
ginerjm Posted June 30, 2013 Share Posted June 30, 2013 What errors? Quote Link to comment https://forums.phpfreaks.com/topic/279707-variable-error/#findComment-1438657 Share on other sites More sharing options...
prathameshkakade Posted June 30, 2013 Author Share Posted June 30, 2013 What errors? The variable $a ij not displayed.I mean the variable is set to the choice which you select and it should display it when I echo $a.But it does not happen. Quote Link to comment https://forums.phpfreaks.com/topic/279707-variable-error/#findComment-1438662 Share on other sites More sharing options...
ginerjm Posted June 30, 2013 Share Posted June 30, 2013 Do you get the rest of the echo statement? Quote Link to comment https://forums.phpfreaks.com/topic/279707-variable-error/#findComment-1438687 Share on other sites More sharing options...
prathameshkakade Posted June 30, 2013 Author Share Posted June 30, 2013 Do you get the rest of the echo statement? Yes. Quote Link to comment https://forums.phpfreaks.com/topic/279707-variable-error/#findComment-1438693 Share on other sites More sharing options...
ginerjm Posted June 30, 2013 Share Posted June 30, 2013 Plain old debugging methods then. Put some echos thru your code to see where it goes and make sure it is doing what you think it's supposed to do. Quote Link to comment https://forums.phpfreaks.com/topic/279707-variable-error/#findComment-1438695 Share on other sites More sharing options...
prathameshkakade Posted June 30, 2013 Author Share Posted June 30, 2013 Plain old debugging methods then. Put some echos thru your code to see where it goes and make sure it is doing what you think it's supposed to do. I didnt understand.Please show me the code for it. Quote Link to comment https://forums.phpfreaks.com/topic/279707-variable-error/#findComment-1438698 Share on other sites More sharing options...
ginerjm Posted June 30, 2013 Share Posted June 30, 2013 Really? Debugging. The practice of putting echo statements in sections of your code so you can check what has happened. Echo $var; Echo "Got to this point". etc. Quote Link to comment https://forums.phpfreaks.com/topic/279707-variable-error/#findComment-1438708 Share on other sites More sharing options...
insidus Posted July 2, 2013 Share Posted July 2, 2013 The first thing you should do before the if(isset($_POST['posted'])) Is add this line var_dump($_POST['posted']); Quote Link to comment https://forums.phpfreaks.com/topic/279707-variable-error/#findComment-1439120 Share on other sites More sharing options...
kicken Posted July 2, 2013 Share Posted July 2, 2013 (edited) Variables do not carry over from one request to another. The request in which you define $a, is not the same request in which you are attempting to echo it. During the request you try and echo $a, you never define it because isset($_POST['posted']) is false for that request. You need to embed $a into a hidden input in your forms in order to pass it along from request to request. When you need it again in the last request, pull it out $_POST array again. Edited July 2, 2013 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/279707-variable-error/#findComment-1439136 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.