Jump to content

Text input correct = go to one page, Text input incorrect = go to another page


Marcus2000

Recommended Posts

Hi people, I really hope you guys can help me out today. I'm just a newbe at php and i'm having real trouble.

 

Bassically all I want to do is have a user type in a company name in a html form.

 

If what the user types in the form matches the company name in my php script i want the user to be sent to another page on my site.

 

If what the user types in the form doesnt match the company name in my php script i want the user to be sent to a differnt page like an error page for example.

 

this is my html form:

 

<form id="form1" name="form1" method="post" action="form_test.php">
  <p>company name: 
    <input type="text" name="company_name" id="company_name" />
  </p>
  <p>
    <input type="submit" name="button" id="button" value="Submit" />
  </p>
</form>

 

And this is the php code I'm trying to process the information on:

 

<?php

$comp_name = abc;

if(isset ($_POST["company_name"])){
if($_POST["company_name"] == $comp_name){

     header("Location: http://www.hotmail.com");
exit();
}

else{
   header("Location: http://www.yahoo.com");
exit();   
}
}
?> 

 

The thing is i'm getting this error when i test it:

Warning: Cannot modify header information - headers already sent by (output started at D:\Sites\killerphp.com\form_test.php:10) in D:\Sites\killerphp.com\form_test.php on line 17

 

Please can some one help me out, i'm sure this is just basic stuff but i just cant get it to work  :shrug:

 

Cheers.

Link to comment
Share on other sites

Your problem is on the very first line of code

$comp_name = abc;

 

That line is trying to assign the value of a constant named abc as the value of $comp_name. I assume you mean to assign a string value of "abc". So, you need to enclose the value in quotes (either single or double)

$comp_name = "abc";

 

EDIT: Also no need for two separate IF statements:

$comp_name = "abc";

if(isset($_POST['company_name']) && $_POST['company_name']==$comp_name)
{
    header("Location: http://www.hotmail.com");
}
else
{
    header("Location: http://www.yahoo.com");   
}

exit();

Link to comment
Share on other sites

Thanks for responding to my post dude, but even with the admenments i am still getting the error message:

 

Warning: Cannot modify header information - headers already sent by (output started at D:\Sites\killerphp.com\form_test.php:10) in D:\Sites\killerphp.com\form_test.php on line 20

 

I'm just not having any luck with this at all. I'm using VertrigoServ for testing the script, is there a chance that it may be an issue with that?

 

this is the exact code i'm testing

 

<?php

$comp_name = "abc";

if(isset($_POST['company_name']) && $_POST['company_name']==$comp_name)
{
    header("Location: http://www.hotmail.com");
}
else
{
    header("Location: http://www.yahoo.com");   
}

exit();
?> 

 

Thanks for your help mate, I know I can get this, its just driving me a little insain now.

Link to comment
Share on other sites

There isn't 20 lines of code present in the post that you made, which is where the error supposedly is.

Are you including this file somwhere? If so, include at the very top with no spaces or outputted text above it.

 

Also when defining variables, do your best to use single quotes, it is proven to be faster.

As when using double quotes, PHP interprets the string to look for any variables present and place them within the string.

 

Regards, PaulRyan.

Link to comment
Share on other sites

Finally..... sucess.

 

Cheers guys, I just removed the code above the php open tag and at the end tag and it seems to be working.

 

Very very pleased, this is the first bit of php code that i have made that seems to work.

 

Thank you so much for your help people.

 

Will be back soon i'm sure, with loads more quetsions.

 

Cheers.

Link to comment
Share on other sites

  • 1 year later...
Guest
This topic is now closed to further replies.
×
×
  • 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.