Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/279707-variable-error/
Share on other sites

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 by kicken
Link to comment
https://forums.phpfreaks.com/topic/279707-variable-error/#findComment-1439136
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.