February 8, 20215 yr Hi all =) newbie here...just trying to figure out PHP for the first time so I can add a Contact Form to my website.... I have been following an article written by my web hosting provider on how to create a basic PHP to email script (Link Here) which is working fine when I copy and paste the code, but when I try to add my own elements (a single checkbox for example), it seems to break the code and gives me 500 errors. I managed to add a single HTML Text Input which emails me the results as required (see $messagesubject in the PHP below), but when I add a checkbox ($sub), I cannot figure out what code I need to use :/ The basic concept of the form is Name, Email, Subject and Message. Then I want to add a small checkbox which if enabled, will let me know via email that the user wants to sign-up for newsletters, offers etc....Initially, I added the "checked" value to the HTML form to set it be default - which works fine - but when the checkbox is manually de-selected, it throws a 500 error at me when the form is submitted? <input type="checkbox" name="sub" checked> This is the HTML I used (above). And the PHP (below) <?php $email_to = "name@domain.co.uk"; $name = $_POST["name"]; $email_from = "contact@domain.co.uk"; $reply_to = $_POST["email"]; $messagesubject = $_POST["messagesubject"]; $message = $_POST["message"]; $sub = $_POST["sub"]; $email_subject = "Message received from Your Site"; $headers = "From: " . $email_from . "\n"; $headers .= "Reply-To: " . $reply_to . "\n"; $message = "Subbed: ". $sub . "\r\nSubject: ". $messagesubject . "\r\nName: ". $name . "\r\nMessage: " . $message; ini_set("sendmail_from", $email_from); $sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from); if ($sent) { header("Location: http://www.yourdomain.co.uk/thankyou.html"); } else { echo "There has been an error sending your comments. Please try later."; } ?> When I added the $sub, it works fine when set as default. The email outputs the "Subbed" as "On". But if the user de-selects it, the 500 error appears. Any help with this would be massively appreciated and sorry if it's such a basic problem I am overlooking....like I said I am fairly new to web development etc/...........Thanks, Vi =)
February 9, 20215 yr Depending on what version of PHP you're using and how strict the errors are set at, it could be failing because in the 'off' state, $_POST['sub'] doesn't exist, so PHP is trying to find a value with the key of 'sub' in the $_POST superglobal, but it can't, and in that case the script is failing. One thing you could do to see this is right after the opening PHP tag, temporailly add var_dump($_POST); die; That will dump the entire output of the $_POST superglobal on the page, so you can see exactly whats going on, in the sub 'on' state, then it will be there, in the 'off' state, it wont. There's a few ways you can fix this using the Null Coalescing Operator, https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce, instead of $sub = $_POST["sub"]; - do $sub = $_POST["sub"] ?? 'off'; - that will default it to 'off' if its not set. Finally, if during local development you're getting 500 errors and you don't know why, then error reporting is turned off in your php.ini file, to get PHP to display errors temporarily, add this snippet to a file, but don't forget to remote it when you're done, you never want to display errors in production! ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
February 9, 20215 yr Author Thank you for the reply jpeters I added the Null operator to the PHP as suggested and it works beautifully now. I did initially setup a workaround using radio buttons but I think the checkbox looks much more clear and simple....so thank you again I wasn't aware of the other features such as var_dump so hopefully these will come in handy when debugging in future (once I learn PHP a lot more lol). As for displaying the errors, I don't think my web host lets me edit the PHP.ini file.....I was looking the other day and for some reason, the PHP info for my server says the .ini file is on my C:/ and not the actual server.....would you happen to know anything about this? Thanks again. Vi
February 9, 20215 yr Author Quick couple of follow-up questions for anyone that can help.....sorry 😇 First, is it possible to change the default values from "On/Off" to "Yes/No" or "Subscribed/Unsubscribed" - for example? And lastly....I promise lol....what is the best way to validate a form of this type? I've done some basic HTML validation such as adding "required" to the tags, but will this still leave security holes in a form like this one? If so what's my best course of action here? Thanks again 😵 Edited February 9, 20215 yr by VioletRose
Create an account or sign in to comment