14 Replies - 1030 Views - Last Post: 30 July 2009 - 08:13 PM Rate Topic: -----

#1 Austinkir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 20-March 09

simple if/then statment not working

Posted 30 July 2009 - 04:44 PM

Greetings!

I'm trying to redirect users to a specific page if they don't enter their name into a form using if/then statements. I have the value for the form set at "first & last name", so here is my php code the form posts to:
<?php
session_start();
$_SESSION['Name']=$_POST['Name'];
$_SESSION['radio']=$_POST['radio'];
$_SESSION['Amount']=$_POST['Amount'];

if ($Name == "first & last name") {
  header ("Location:rsvp_name.php"); }

if ($radio == "NO"){
  header ("Location:rsvp_2_no.php"); }
else{
header ("Location:rsvp_2.php");  }

?>


HTML part:
 <? if ($FINISHED == "YES"){
		print "<p>Please only submit one responce. If you made an error, please call Tammy at 530.626.1492. Thank you.</p><p>I have already responded, <br><a href=\"index.html\">continue browsing.</a></p>";}
		else {
		print "<br><br><form method=\"post\" action=\"responce.php\"><input type=\"radio\" name=\"radio\" value=\"YES\" checked=\"checked\" class=\"styled\"> <img id=\"rsvp_1\" src=\"../images/rsvp_1.png\" style=\"z-index:-1;\"> <input type=\"text\" size=\"1\" name=\"Amount\" id=\"amountbox\" class=\"inputbox\"><br><input type=\"radio\" name=\"radio\" value=\"NO\" class=\"styled\"> <img id=\"rsvp_2\" src=\"../images/rsvp_2.png\"><a href=\"index.html\"><img id=\"rsvp_3\" src=\"../images/rsvp_3.png\"></a><br><br><input type=\"text\" style=\"color: #AAA;\" value=\"first & last name\" size=\"35\" class=\"inputbox\" id=\"namebox\" name=\"Name\" onclick=\"clickedOnce(this);\"><td colspan=2 align=center><input class=\"button\" type=submit name=\"submit\" value=\"Next\"></td></form>"; }
		?>


Here is where the site is located: www.AndreaAndJordan.net/rsvp/
Just wait a few seconds after the page loads for the info to fade into view.

When I hit "Next" without editing the name, I want to be redirected to rsvp_name.php, but it just continues on as if I had edited the name. The other if/then statements work as they should.
"first &amp; last name" must not equal $Name. Any body know why?

Is This A Good Question/Topic? 0
  • +

Replies To: simple if/then statment not working

#2 noname_clark  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 75
  • Joined: 22-October 08

Re: simple if/then statment not working

Posted 30 July 2009 - 05:09 PM

you never define $name.

try this:

<?php
session_start();
$_SESSION['Name']=$_POST['Name'];
$_SESSION['radio']=$_POST['radio'];
$_SESSION['Amount']=$_POST['Amount'];

$Name=$_POST['Name'];
$radio=$_POST['radio'];
$Amount=$_POST['Amount'];


if ($Name == "first & last name") {
  header ("Location:rsvp_name.php"); }

if ($radio == "NO"){
  header ("Location:rsvp_2_no.php"); }
else{
header ("Location:rsvp_2.php");  }

?>


Was This Post Helpful? 0
  • +
  • -

#3 Austinkir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 20-March 09

Re: simple if/then statment not working

Posted 30 July 2009 - 05:23 PM

Why then does the other conditional statement work? By starting a session and using $_SESSION['Name'] isn't that defining it?

I tried your code, and it had the same effect (acted like $Name did not == "first & last name")

This post has been edited by Austinkir: 30 July 2009 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 noname_clark  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 75
  • Joined: 22-October 08

Re: simple if/then statment not working

Posted 30 July 2009 - 05:35 PM

i'm pretty sure doing $_SESSION['Name'] doesn't define $Name too, but I could be wrong, I don't like sessions, I prefer cookies. but you should try this now:

<?php
session_start();
$_SESSION['Name']=$_POST['Name'];
$_SESSION['radio']=$_POST['radio'];
$_SESSION['Amount']=$_POST['Amount'];

echo $Name; /*This will tell you what $Name's value is*/


if ($Name == "first & last name") {
  header ("Location:rsvp_name.php"); }

if ($radio == "NO"){
  header ("Location:rsvp_2_no.php"); }
else{
header ("Location:rsvp_2.php");  }

?>



make sure that it says "First & Last name"

if it's not, then I would suspect it might be the javascript you're using to make the "first & Last name" disappear when you click it, so try removing that javascript and see if the same thing happens.
Was This Post Helpful? 0
  • +
  • -

#5 Austinkir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 20-March 09

Re: simple if/then statment not working

Posted 30 July 2009 - 06:03 PM

That was a great idea, thanks for your help.

However it echoed exactly what it should have: "first and last name" (I changed it thinking perhaps the "&" was throwing it off).

Any other ideas? Or, any other way of accomplishing this? Although I'd really like to know why this method is not working.
Was This Post Helpful? 0
  • +
  • -

#6 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Re: simple if/then statment not working

Posted 30 July 2009 - 06:19 PM

Well it really depends on what you have... Of course, now you've stored your $_POST['name'] into a session variable, but as for this...

if ($Name=="first and last name")


It seems to me that only if $Name is (exact text) "first and last name", then it would fail to execute.

But just to play it safe, I'd suggest the following:

 $Name=$_SESSION['name']; 


It might seem redundant, but static association has always worked for me. (Rather than relying on what I call "fuzzy functions" where things aren't so black-and-white)
Was This Post Helpful? 0
  • +
  • -

#7 Austinkir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 20-March 09

Re: simple if/then statment not working

Posted 30 July 2009 - 06:37 PM

Hmm... I did add the static association as you suggested, but it did not work.

Just to see if it worked, I added $Name=="first and last name";, and it did not redirect me. This leads me to believe that the problem lies in the header ("Location:rsvp_name.php"); }

It seems so simple.... any other ideas?
Was This Post Helpful? 0
  • +
  • -

#8 Austinkir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 20-March 09

Re: simple if/then statment not working

Posted 30 July 2009 - 06:45 PM

OK so I just changed the code to this:
 
<?php
session_start();
$_SESSION['Name']=$_POST['Name'];
$_SESSION['radio']=$_POST['radio'];
$_SESSION['Amount']=$_POST['Amount'];

if ($Name == "first and last name") {
  header ("Location:rsvp_name.php"); }
else{
header ("Location:rsvp_2.php");  }

?>


and it redirected me where it is supposed to. Is an else statement required for an if statement, or is there a limit to 1 if/then per file? It seems that in this code:
<?php
session_start();
$_SESSION['Name']=$_POST['Name'];
$_SESSION['radio']=$_POST['radio'];
$_SESSION['Amount']=$_POST['Amount'];

if ($Name == "first and last name") {
  header ("Location:rsvp_name.php"); }

if ($radio == "NO"){
  header ("Location:rsvp_2_no.php"); }
else{
header ("Location:rsvp_2.php");  }

?>


PHP simply ignores the first IF.

?
Was This Post Helpful? 0
  • +
  • -

#9 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Re: simple if/then statment not working

Posted 30 July 2009 - 07:22 PM

It stops you at the first IF because it redirects you. Upon redirection, the server stops processing the file and moves on. (It does the same if you redirect with javascript)

As for multiple else statements, you can if it's an "elseif". E.g.

if ($this=="that") {
  //Do This
} elseif ($that=="this") {
  //Do That
} else {
  //Do whatever....
}


However, else statements are not required. If the "if/then" statement fails and there is no "else", you can leave if statements without else's.
Was This Post Helpful? 0
  • +
  • -

#10 Austinkir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 20-March 09

Re: simple if/then statment not working

Posted 30 July 2009 - 07:53 PM

Thanks for the reply.

I don't think were on the same page.

this code:
<?php
session_start();
$_SESSION['Name']=$_POST['Name'];
$_SESSION['radio']=$_POST['radio'];
$_SESSION['Amount']=$_POST['Amount'];

if ($Name == "first and last name") {
  header ("Location:rsvp_name.php"); }

if ($radio == "NO"){
  header ("Location:rsvp_2_no.php"); }
else{
header ("Location:rsvp_2.php");  }

?>



Is NOT redirecting me to rsvp_name.php, as it logically should. Any idea why?

This post has been edited by Austinkir: 30 July 2009 - 07:54 PM

Was This Post Helpful? 0
  • +
  • -

#11 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Re: simple if/then statment not working

Posted 30 July 2009 - 07:53 PM

Yes. $Name does not equal "first and last name". Try looking at the format in which you put $Name. If necessary, echo $Name back at yourself to check for errors. However, it also might be a simple issue of case. For example "First and Last name" will NOT equal "first and last name". Without more to work with, you're leaving us in the dark.
Was This Post Helpful? 0
  • +
  • -

#12 Austinkir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 20-March 09

Re: simple if/then statment not working

Posted 30 July 2009 - 07:57 PM

Oh, I'm sorry the1corrupted, I should have analyzed your post more before replying, changing the second if to elseif works!

Thank you very much. :)

I did try an echo of $Name, and it was exactly as it should be (I copied/pasted). So its supposed to work without changing the second one to elseif?
Was This Post Helpful? 0
  • +
  • -

#13 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Re: simple if/then statment not working

Posted 30 July 2009 - 08:00 PM

Well you're welcome. I'm glad I could help. However, you're comparing two very different values...

Though logically, yes, the first "If" should execute and send you on your merry way, but since the elseif works, use it. However, it might be due to conflicting header statements. I usually use javascript to redirect, and its behavior is predictable. If I redirect with JS, then the page stops processing and boots you around.
Was This Post Helpful? 0
  • +
  • -

#14 Austinkir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 20-March 09

Re: simple if/then statment not working

Posted 30 July 2009 - 08:06 PM

Hmm, interesting. I think I'll read up on headers sometime as I don't understand them.
Thanks for the tip on using javasript. Would you personally include the js in the file that was posted to (where all the php is) or in the html file where the form is?
Was This Post Helpful? 0
  • +
  • -

#15 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Re: simple if/then statment not working

Posted 30 July 2009 - 08:13 PM

Usually I use PHTML. It's fun, easy, and it tends to not muss up your code with so many echo or print statements. In fact, I have a php page that's almost entirely html except for a $_SERVER['REMOTE_ADDR'] to get the user's IP address.

As in...

<?PHP

//PHP Goes HERE

?>

<!--HTML GOES HERE-->

<?php

//MORE PHP (if you want)

?>


But whatever you choose to do is up to you.

And on a side note: You can include a .html file. The way PHP regards an include is a literal and direct insertion of the code. Where you include is where it shows up. (When you include an html file, no special code is required to get it to display)

This post has been edited by the1corrupted: 30 July 2009 - 08:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1