- This topic is empty.
-
AuthorPosts
-
October 4, 2014 at 1:56 am #185402
smarty.rockz
ParticipantHey,
I am building a website which has a form and have multiple check boxes, I want that when the user submit the form, all the check boxes that have been checked, those values get stored and sent to the email-id, so for this do I have to create individual string for individual value or can it be stored in a single string.
This is my sample HTML:div class="checkbox"> <label> <input type="checkbox" name="subject" value="I am interested in franchising."> I am interested in franchising. </label> </div> <div class="checkbox"> <label> <input type="checkbox" name="subject" value="Lets have some coffee & connect personally."> Lets have some coffee & connect personally. </label> </div> <div class="checkbox"> <label> <input type="checkbox" name="subject" value="Others"> Others. </label> </div>
And this is my PHP, will this php work?
$name =""; // Sender Name $email =""; // Sender's email ID $message =""; // Sender's Message $subject =""; // Sender's Subject $subjectError =""; $nameError =""; $emailError =""; $messageError =""; if (empty($_POST["subject"])) { $subjectError = "Subject is required"; } if(mail($email, $headers, $msg ) && mail("[email protected]", $header, $msg1 )) { $successMessage = "Message sent successfully. You will be contacted soon."; } } else { $emailError = "Invalid Email"; } } }
October 4, 2014 at 3:00 am #185404nigelc
ParticipantHave a read of this:
http://www.html-form-guide.com/php-form/php-form-checkbox.html
Read through the other tutorials on there it will give you a good basic insight into building and handling forms.
October 4, 2014 at 9:16 am #185428__
ParticipantAnd this is my PHP, will this php work?
Have you tried it? did it work?
If I understand you correctly, you wish to allow multiple subjects. Is this correct? If so, your current code will only retrieve the value of the last checkbox (because each checkbox has the same name). The solution would be to use square brackets (
[]
) in your html so PHP knows you want multiple values:<input type=checkbox name=subject[] value=…>
Then, in your PHP script, instead of a string, you’d end up with an array of strings. You could use implode to join them together into a single string for your email:
$subject = implode( $subject,"\n" );
Your sample code does not show how you are retrieving the values from your form (i.e.,
$_GET
or$_POST
),* but be aware that you need to validate at least the email the user submits. If you do not make sure it is a single email address, your form will be open to header injection attacks (an attacker could use your form —and your web hosting account, and your good name— to send spam to thousands of others).* I hope you are doing so, and that you have not configured PHP with
register_globals
. If variables from your form magically “show up” in your script, it is something you need to disable immediately. It opens up a very dangerous security vulnerability. You should always use the$_GET
or$_POST
arrays to get input from your users, and make sureregister_globals
is disabled (or, upgrade to a more recent version of PHP, where this setting has been removed completely).October 4, 2014 at 9:41 am #185432smarty.rockz
ParticipantActually, I am not so into PHP, I just got this code from the web and just edited to suit my needs, so I don’t know anything about POST or GET but I saw the code and Post is mentioned here :
if (empty($_POST["subject"])) { $subjectError = "Subject is required"; }
and also in my html:
<form role="form" action="validation.php" method="post">
and also do I have to add “[]” to “subject” even in PHP?
And I did this in my PHP, about what you said implode:
$subject =implode($subject, "\n"); // Sender's Subject $subjectError ="";
October 4, 2014 at 11:37 am #185446__
Participant…I don’t know anything about POST or GET but I saw the code and Post is mentioned here
So, there is no code that assigns those variables? anything like
$email = $_POST['email'];
?
→Again, have you tried this script yet? what happens?
and also do I have to add “[]” to “subject” even in PHP?
Meaning,
$subject[]
? No, that means something else. The brackets on the htmlname
simply let PHP know that you want an array of values instead of a single value.→ What version of PHP are you running? You can find this info from your web host, or by creating a PHP script like this:
<?php phpinfo();
…and visiting the page in your browser. This will also give you lots of other useful info about your PHP installation. (Don’t leave this script on your website, however; it’s not information you want to be publicly available.)
October 4, 2014 at 12:24 pm #185449smarty.rockz
ParticipantI am developing the website locally, and test it through dropbox, and dropbox does not supports php, so can’t test the script nor can know the php version.
There is this :if (empty($_POST["email"])) { $emailError = "Email is required"; } else { $email = test_input($_POST["email"]);
October 4, 2014 at 1:29 pm #185453__
ParticipantThat snippet makes me worry about how long ago the script was written, and how well. Would you post the entire script (perhaps make a gist on github)?
I am developing the website locally, and test it through dropbox, and dropbox does not supports php, so can’t test the script nor can know the php version.
Install your own server with PHP locally. There are software packages that make it easy (like xampp). There’s no point in writing PHP code if you can’t run/test it.
October 5, 2014 at 7:32 am #185488smarty.rockz
ParticipantThis is the link:
https://gist.github.com/0dc44d3440cee8de6150.git
I tried testing it with xampp, but when clicked submit the whole php comes and no email gets sent.
October 5, 2014 at 11:00 am #185501__
Participantwhen clicked submit the whole php comes and no email gets sent.
Meaning, you saw all of the PHP code on your browser? did you test your installation to make sure it works (you should see a page titled “It Works!”)?
This is the link: https://gist.github.com/0dc44d3440cee8de6150
Yeah… don’t use that.
Here’s a better starting point.October 5, 2014 at 11:29 am #185505smarty.rockz
ParticipantGot your code, I wanted to ask a question, in the document that you gave, after ending your php, you added the html code, so do I have to add my html form code in my contact-me.php , because I am using an external sheet for the php code, or just the php excluding the html part?
October 5, 2014 at 11:53 am #185506__
ParticipantYou can put them in separate files if you prefer. You’ll need to change the form’s
action
to match the php script’s new URL, of course. You would also need to change the way you display the success/error messages, but that’s fine too.October 5, 2014 at 12:28 pm #185510smarty.rockz
ParticipantOk, I tried the code, last time when I used to click the submit button, it would just show the code.
Now until I fill up the name,email or message fields, it won’t proceed ahead, I think so because I added “required” at the end of my input tag as was mentioned in your code, when I fill up those and press submit, a blank screen comes, this time the code is not showing nor I am receiving the email but for the check boxes it isn’t showing any error, I want that at least checking one check box should be necessary/required, and the errors that we had included:
<p class=result><?php echo $result ?></p>
Even this error is not showing, so I exactly don’t know whether it worked or not, though I feel it will when I will host the site.October 5, 2014 at 1:04 pm #185513__
Participantwhen I fill up those and press submit, a blank screen comes
Did you separate the html and php parts? If so, the PHP part won’t be producing any output on its own. For now, at the very bottom of the PHP script, you can add:
echo $message;
…and you should get one of the three messages defined in the “configuration” section.
I want that at least checking one check box should be necessary/required
We do require at least one checked box, but we only check that once the form is submitted.
There’s a couple ways you could do this on the browser. The most straightforward would be to use a select list instead of a group of checkboxes:
<select name=subject[] multiple required> <option>I am interested in franchising.</option> <option>Let's have some coffee & connect personally.</option> <option>Other.</option> </select>
This is the “perfect” and “correct” solution. Unfortunately, multi-select boxes are uncommon and many users don’t understand how to use them.
The other would be using javascript when the form is submitted. More complicated to build/understand, but easier to use:
http://codepen.io/adrian-enspired/pen/JqHjy
<p class=result><?php echo $result ?></p>
Even this error is not showing, so I exactly don’t know whether it worked or notI don’t know where this is. It’s not in my version, and it doesn’t seem to be in yours either. If you’re trying to do debugging,
var_dump
is usually more useful thanecho
. -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.