blink359 Posted November 17, 2010 Share Posted November 17, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/218946-ifs-not-working-properly/ Share on other sites More sharing options...
johnsmith153 Posted November 17, 2010 Share Posted November 17, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/218946-ifs-not-working-properly/#findComment-1135456 Share on other sites More sharing options...
johnsmith153 Posted November 17, 2010 Share Posted November 17, 2010 ... and, you've only used one = in your if statements. I nearly missed that. if($subject="Enquiry") if($subject=="Enquiry") Quote Link to comment https://forums.phpfreaks.com/topic/218946-ifs-not-working-properly/#findComment-1135459 Share on other sites More sharing options...
blink359 Posted November 17, 2010 Author Share Posted November 17, 2010 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/218946-ifs-not-working-properly/#findComment-1135494 Share on other sites More sharing options...
PFMaBiSmAd Posted November 17, 2010 Share Posted November 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/218946-ifs-not-working-properly/#findComment-1135500 Share on other sites More sharing options...
Anti-Moronic Posted November 17, 2010 Share Posted November 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/218946-ifs-not-working-properly/#findComment-1135504 Share on other sites More sharing options...
blink359 Posted November 17, 2010 Author Share Posted November 17, 2010 ahh thanks, i've never done something like this ive only had to use != so far in ifs so i didnt know, it works now nad i have learnt 2 new things today Quote Link to comment https://forums.phpfreaks.com/topic/218946-ifs-not-working-properly/#findComment-1135512 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.