Jump to content

if's not working properly


blink359

Recommended Posts

Hi there, I have created an email script and i want it to email a different email depending on the subject chosen by raido boxes however the if's aren't doing what they should, If i select say recruitement it choses the last option and has the wrong subject here is my code:

<html>
<head>
</head>
<body>
<form action="contact.php" method="post">
<fieldset>
<legend>Contact Us</legend>
Your Email:*<br>
<input type="text" name="email"><br>
Subject:*<br>
Recruitment Enquiry:<input type="radio" name="subject" value="Recruitment">
Absense Notification:<input type="radio" name="subject" value="Absense">
General Enquiry<input type="radio" name="subject" value="Enquiry">
<br>
Message:*<br>
<textarea name="message" cols="50" rows="5"></textarea><br>

<input type="submit" value="Send Email">
</form>
Required fields are marked with a *<br><br>
<?php
if(isset($_POST))
{
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

//checcking that all relevent information is entered and correct

if(!$email || !$message)
{
	$errmessage ="Please fill in all required fields.";	
}
if($subject="Recruitment")
{
	$to="email1";	
}	
if($subject="Notification")
{
	$to="email2k";
}	
if($subject="Enquiry")
{
	$to="email3";	
}
//Sending the email if nothing is wrong

if(!$errmessage)
{
header("location:send.php?to=".$to."&subject=".$subject."&email=".$email."&message=".$message."");
}else{
	echo $errmessage;
}
}
?>
</body>
</html>

If anyone can help it would be greatly appriciated

 

Thanks,

 

blink359

Link to comment
https://forums.phpfreaks.com/topic/218946-ifs-not-working-properly/
Share on other sites

 

if($subject="Notification"), yet you've used value="Absense".

 

I would change the radio button values to 1, 2, 3.

 

So..

Recruitment Enquiry:<input type="radio" name="subject" value="1">
Absense Notification:<input type="radio" name="subject" value="2">
General Enquiry<input type="radio" name="subject" value="3">

...don't forget to change the processing script!

It's not the html its the php i moved half the script to another one and the drop down passed on the correct value the if statements are changing the value

 

Script 1:

<html>
<head>
</head>
<body>
<form action="contact.php" method="post">
<fieldset>
<legend>Contact Us</legend>
Your Email:*<br>
<input type="text" name="email"><br>
Subject:*<br>
<select name="subject">
<option value="1">Recruitment</option>
<option value="2">Absense</option>
<option value="3">Enquiry</option>
</select>
<br>
Message:*<br>
<textarea name="message" cols="50" rows="5"></textarea><br>
Please type the code shown in the image:<br><script type="text/javascript" src="http://webspamprotect.com/captcha/3096/"></script>
<noscript>This form protected by <a href="http://webspamprotect.com" target="_blank" title="Web form spam protection">WebSpamProtect</a>. JavaScript must be enabled in your browser to view this image.
</noscript> <input type="text" name="wsp_code"/><br>
<input type="submit" value="Send Email">


</form>
Required fields are marked with a *<br><br>
<?php
if(isset($_POST))
{
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "[email protected]";

//checcking that all relevent information is entered and correct



include_once("wsp_captcha.php");

if (WSP_CheckImageCode() != "OK") 
{
   		 $errmessage="The image code you have entered is incorrect.";
}

//Sending the email if nothing is wrong

if(!$errmessage)
{
header("location:send.php?to=".$to."&subject=".$subject."&email=".$email."&message=".$message."");
}else{
	echo $errmessage;
}
}
?>
</body>
</html>

 

Script 2:

<?php
$to = $HTTP_GET_VARS["to"];
$subject = $HTTP_GET_VARS["subject"];
$message = $HTTP_GET_VARS["message"];
$email = $HTTP_GET_VARS["email"];
if(!$email || !$message)
{
	$errmessage ="Please fill in all required fields.";	
}
if($subject="1")
{
	$to="[email protected]";	
}	
if($subject="2")
{
	$to="[email protected]";
}	
if($subject="3")
{
	$to="[email protected]";	
}	
if($subject="1")
{
	$subject="Recruitment";	
}	
if($subject="2")
{
	$subject="Absense";
}	
if($subject="3")
{
	$subject="Enquiry";	
}
	mail($to, $subject, $message ,"From: $email"); 
echo("Email Sent");
?>

One equal sign = is an assignment operator and except when you are assigning a zero/null/false value on the right-hand side of the statement, assignment statements are always true.

 

All your if($var = 'string') are assigning the string to the $var and testing the result of that assignment.

 

Two equal signs == is a comparison operator.

Yeh, you need to ==

 

Also, I would advise a switch statement for your dynamic assignment of the $to email:

 


switch($subject){

  case 'Recruitment':
    $to ='email1';
    break;

//etc

}

 

..easier to manage and modify and doesn't perform any unnecessary comparisons.

Archived

This topic is now archived and is 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.