Trouble with if...else Statement

I’m making a form where users will input their data using radio buttons and drop down tables then submit it. Depending on what they submitted I want to return specific data. I figured I could just use the if…else statement in PHP since a database is not really needed (and would take longer to setup). However, I’m fairly new to PHP and am having some trouble with the if…else statement. First off, I am doing this in a Wordpress Page. So in the Wordpress HTML Editor I have this:

<form action=“post”>
<select name=“model”>
<option value=“select”>-Select Option-</option>
<option value=“Phone 1”>Phone 1</option>
<option value=“Phone 2”>Phone 2</option>
<option value=“Phone 3”>Phone 3</option>
<option value=“Phone 4”>Phone 4</option>
</select>
</form>

<form method=“post”>
<select name=“color”>
<option value=“select”>-Select Option-</option>
<option value=“White”>White</option>
<option value=“Black”>Black</option>
<input type=“submit” value=“Submit” />
</select>
</form>

<? include(‘process.php’); ?>

In the file “process.php” I have this:

<?php
$c=color(“C”);
if ($c==“White”)
echo “Your phone is not black”;
else
echo “Your phone is not white”;
?>

But having that PHP code in the file process.php returns this error:

Call to undefined function color() in public_html/process.php on line 10

If I remove that line of code it just writes “your phone is not white”.

So what am I doing wrong?

Thanks in advance :wave:

To get the color you need to use:

$c = $_POST['color'];

Assuming it was sent to the page as a post variable (as it seems to be), this would assign the value sent to the page as $c.

Hope that helps

Thanks that worked but it writes the color before you hit submit. Do you know how I can stop it so it prints the color once the user clicks submit and not before?

Thanks;)

In that case you should check to see if the post data has been sent and then display it, try putting the code processing the input into an if statement:

If(isset($_POST['color'];)) {
...code here
}

the isset() function just checks to see if a variable of that name exists.

If when you reload the page your browser resends the form then it will do that. If you go to the address bar and hit enter on the url you should see that it doesn’t resend the data and therefore doesn’t display the colour info. If you are resending the form when reloading there is no way around this because as far as the system is concerned it’s an identical action.

If it’s not the resending of the form that is the problem you could use a session variable, that’s a little more complicated but will allow you to save a variable that says the colour has already been sent:

<?php

session_start() // this must be at the top of each page using a session (before any data is sent to the browser)

if(isset($_POST['color']) && $_SESSION['color_sent'] != true){   // the second part here checks the session var hasn't been set previously ie becuase the page has displayed the colour
$_SESSION['color_sent'] = true;   //this line sets a session variable that is stored for users visit
$c=$_POST['color'];

if ($c=="White"){
echo "Your phone is not black";
}else{
echo "Your phone is not white";
}

}

?> 

Of course the code above would only let the user view the colour once even if they went back and resubmitted the form.

Try this as the process.php


<?php
if(isset($_POST['color'])){
   $c=$_POST['color'];

   if ($c=="White"){
      echo "Your phone is not black";
   }else{
      echo "Your phone is not white";
   }

}

?>

Try this as the process.php

Code:
<?php
if(isset($_POST[‘color’])){
$c=$_POST[‘color’];

if ($c==“White”){
echo “Your phone is not black”;
}else{
echo “Your phone is not white”;
}

}

?>

Thanks a million, that fixed the problem. One more question though (I know, I know I’m a noob =P). Once you click submit the text stays on the page, this isn’t a problem but it doesn’t go away even when you reload the page. How can I have the text disappear when the page is re-loaded? Could I use a loop so that it just starts the code over again when the page gets reloaded?

Thanks:)

Keeps returning this error:

Parse error: syntax error, unexpected T_IF in public_html/process.php on line 13

Its this part that is sending the error (I believe):


if(isset($_POST['color'])

But removing that will display the text before the user clicks submit.

sorry my mistake, there needs to be a semicolon after the session_start(); statement

Once I added the semicolon it worked, once, but know it won’t work. Cleared cache, left site, etc. I also don’t get a window that asks to re-send form anymore, maybe that’s why.

Are you resending the form when you reload the page? some browsers eg opera do this automatically when hitting refresh others ask you to resend the data.

If you’re resending the form (even as part of a refresh) ultimately PHP only sees that the form has been submitted and has to process it.

Yes, when using Safari I am asked to resend the form (which I do). And in Firefox it does it automatically.

If you’re resending the form (even as part of a refresh) ultimately PHP only sees that the form has been submitted and has to process it.

So is there anyway to work around that or will it just be that way?

No, but then if people reload the page and choose to resend the form it should show exactly the same as it did the time before so everything is working as you’d expect. Unless I’m missing what you’re trying to achieve.

Let me elaborate on what is going on, once I put the code (the new code containing session_start) into the process.php file, I uploaded it to my server and went to the webpage. I selected an option from the drop down list and clicked submit. Everything worked as it should and it displayed the text underneath the drop down list. When I refreshed the page (re-sent the form) the text went away (like I originally wanted it to). But when you select an option again once the page has been reloaded no text shows at all. So is what’s happening right or is there something wrong?

Sorry for the confusion.

That is what I would expect it to do, the session remains after refreshing the page and the code is set to only show the text if the session variable is not set.

I’m not sure why you want the text to disappear if you refresh the page though, are you expecting users to hit refresh after viewing your page. To me stopping it from showing the text seems strange, if they follow a link back to the page the text will disappear naturally as the text is set to only appear when the form is submitted. So I don’t think you should be using the session code.

I removed the session code and now its doing what I wanted it to do in the first place. I think it was a wordpress cache problem because I was re-uploading files and changing code so much in a short period of time.

Thanks for the help.