I can not get the form check boxes to come through to the email for "recycling". I do not know php so I have no idea what is wrong. When the form comes through to email the "Recycling:" subject is there, but the checked boxes are not.
HTML:
<label>CRT Monitors <input name="recycleobject2[]" type="checkbox" value="crtmonitors"
/></label>
<label>Printers <input name="recycleobject2[]" type="checkbox" value="printers"
/></label>
<label>Computers <input name="recycleobject2[]" type="checkbox" value="computers"
/></label>
<label>Fluorescent Lamps and Batteries <input name="recycleobject2[]" type="checkbox"
value="lamps" /></label>
<label>Televisions <input name="recycleobject2[]" type="checkbox" value="television"
/></label>
<label>Other Equipment <input name="recycleobject2[]" type="checkbox" value="other" />
</label><br />
PHP:
$recycleobject2 = $_POST['recycleobject2']; // not required
$recycleobject2 = array();
$email_message .= "Recycling:" .implode(", ",$recycleobject2)."\n";
your problem is right here:
$recycleobject2 = array();
That line is resetting the $recycleobject2 variable to a brand new empty array. Remove that line and your code should be fine.
Related
I am trying to get this information into a text file using PHP.
All of the websites that I have looked at only really cover entering a first and a last name had having the information echoed.
I was wondering if anyone has a suggestion on how to do this? That would be very helpful.
<form action="action.php" method="post" />
Please indicate if you us Mathworks MATLAB: <br>
<input type="radio" name="question" value="yes" checked> Yes<br>
<input type="radio" name="question" value="no"> No<br>
If yes, please indicate which of these currently purchased toolboxes you use $
form action="action.php" method="post" />
Please indicate if you us Mathworks MATLAB: <br>
<input type="radio" name="question" value="yes" checked> Yes<br>
<input type="radio" name="question" value="no"> No<br>
If yes, please indicate which of these currently purchased toolboxes you use $
<input type="checkbox" name="tool" value="Control">Control Systems Toolbox<br>
<input type="checkbox" name="tool" value="Image">Image Processing Toolbox<br>
<input type="checkbox" name="tool" value="Optimiz">Optimization Toolbox<br>
<input type="checkbox" name="tool" value="Robust">Robust Control Toolbox<br>
<input type="checkbox" name="tool" value="Signal">Signal Processing Toolbox<br>
Please enter a comma separated list of toolboxes you would like to use for you$
<input type="text" name="textquestion" value=""><br>
<input type="submit" value="Submit">
This is fairly straightforward,
$file = "myfile.txt";
if (isset($_POST['question']) && isset($_POST['tool'])) {
$fh = fopen($file, 'a');
$text = $_POST['question']. ' ' .$_POST['tool'];
fwrite($fh,$text); // Write form data to the file
fclose($fh); // Close the file
}
This would be put in your action.php file, and would write to a file in the same directory.
Also a caveat, you have input names that aren't unique, so you would want to add a third field to this. 'question' should be a unique name as you're using it when posting data.
I am using the code below to ask users to rank which programming language they are more comfortable with.
The users need to rank from 1-3 (1 being the one they are most comfortable with)
<form id="form1" name="form1" method="post" action="">
<input type="number" name="php" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="python" required="required" max="3" min="1"/>Python <br />
<input type="number" name="ruby" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
Once the user prioritizes the programming languages and hits submit, how can I on the next page echo the ranking selection? (e.g. Your first choice is x, your second choice is y and your third choice is z)
I would do it like so (Note that I've changed the the value of the name attributes on the form elements):
<form id="form1" name="form1" method="post" action="">
<input type="number" name="lang[php]" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="lang[python]" required="required" max="3" min="1"/>Python <br />
<input type="number" name="lang[ruby]" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
And in the php:
//Get the form results (which has been converted to an associative array) from the $_POST super global
$langs = $_POST['lang'];
//Sort the values by rank and keep the key associations.
asort($langs, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($langs as $lang => $rank)
{
//echo out here first, second, and third rank with each iteration respectively.
}
The asort function simply sorts the array by value while maintaining key association.
I am not sure that tag input type="number" exists.
you do better
<legend>
<label><input type="radio" name="php" value="1">1</label>
<label><input type="radio" name="php" value="2">2</label>
<label><input type="radio" name="php" value="3">3</label>
</legend>
<legend>
<label><input type="radio" name="python" value="1">1</label>
<label><input type="radio" name="python" value="2">2</label>
<label><input type="radio" name="python" value="3">3</label>
</legend>
you must not use 'required' attribute for radio tag or checkbox tag
so you make a check javascript function whether radio box is checked or not.
<form name..... onsubmit = "return check_submit();">
<script>
var check_submit = function(){
if($("input[name=php]:checked").val() =="")
return false;
...
return true;
}
</script>
or you can use
<input type="text" name="php">
then on next page you can do like this
$php = intval(trim($_POST['php']));
$python = intval(trim($_POST['python']));
$msg = "your first choice for php is '.$php;
$msg.="your second choice for phthon is '.$python;
.....etc..
I have the following code, I want the user to be able to sign up to multiple lists at the same time. It is currently only signing up emails to one list randomly, even if I check all the lists. Is it possible to do? Maybe some sort of php echo?
<form action="" method="post">
<input name="accName" type="hidden" value="companyname">
<input name="listName" type="hidden" value="">
<input name="fullEmailValidationInd" type="hidden" value="Y">
<input name="doubleOptin" type="hidden" value="false">
<input name="successUrl" type="hidden" value="">
<input name="errorUrl" type="hidden" value="">
Email Address <input class="border" name="email" size='50' type="text" value="">
First Name <input class="border" name="First_Name" size='50' type="text" value="">
Last Name <input class="border" name="Last_Name" size='50' type="text" value="">
<label><input name="listName" type="checkbox" value="list1"></label>
<label><input name="listName" type="checkbox" value="list2"></label>
<label><input name="listName" type="checkbox" value="list3"></label>
<label><input name="listName" type="checkbox" value="list4"></label>
<label><input name="listName" type="checkbox" value="list5"></label>
<input type="submit" value="OK">
</form>
Use array for the checkboxes:
<input type="checkbox" NAME="listName[]" VALUE="list1" />
Or use different names...
You will need to grab this using, php side, a loop:
foreach ($_POST['listName'] as $selected)
Just notice that if none are selected this will fail, thus check if the array exists before:
if (isset($_POST['listName'])
{
foreach ($_POST['listName'] as selected)
{
DO YOUR STUFF
}
}
What should I type in PHP instead of I got now to receive in mail all of the answers typed in checkboxes by user. Now I have only one. Thanks
PHP:
$email_message .= "Sport: ".clean_string($_POST["sport"])."\n";
$email_message .= "Music: ".clean_string($_POST["music"])."\n";
HTML:
<div id="Oobj58" class="Oobj">
<input type="checkbox" name="sport" value="Kosz" />koszykówka<br>
<input type="checkbox" name="sport" value="Zimowe" />sporty zimowe<br>
<input type="checkbox" name="sport" value="Konie" />jeździectwo konne<br>
</div>
<div id="Oobj61" class="Oobj">
<input type="checkbox" name="rozrywka" value="festiwal" />festiwale<br />
<input type="checkbox" name="rozrywka" value="wesole" />wesołe miasteczka<br />
<input type="checkbox" name="rozrywka" value="zespolowo"/>paintball,bilard,kręgle..<br/>
</div>
Edit:
Thanks but not exactly can type different 'names' cause I also have one checkboxe to set all on check at once and it works with JS like this:
<div id="Oobj61" class="Oobj">
<script language="JavaScript">
function toggle2(source) {
checkboxes = document.getElementsByName('rozrywka');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
<input type="checkbox" onClick="toggle2(this)" /><br>
<input type="checkbox" name="rozrywka" value="club" />kluby<br />
<input type="checkbox" name="rozrywka" value="puby" />puby<br />
<input type="checkbox" name="rozrywka" value="koncert" />koncerty<br />
<input type="checkbox" name="rozrywka" value="festiwal" />festiwale<br />
<input type="checkbox" name="rozrywka" value="wesole" />wesołe miasteczka<br />
<input type="checkbox" name="rozrywka" value="zespolowo" />paintball,bilard,kręgle...<br
/>
</div>
I'm not 100% sure what it is you're asking here, but if you'd like to see if a checkbox is set you can do the following:
if(isset($_POST['checkbox-name-here'])){
//it's set code here, maybe set a variable...
$var = "checkbox value";}
That's a rough example, but you should get the drift.
If you're trying to get multiples - then give each checkbox a unique name:
<div id="Oobj58" class="Oobj">
<input type="checkbox" name="sport1" value="Kosz" />koszykówka<br>
<input type="checkbox" name="sport2" value="Zimowe" />sporty zimowe<br>
<input type="checkbox" name="sport3" value="Konie" />jeździectwo konne<br>
</div>
Then in PHP you can check if each value is set:
if(isset($_POST['sport1'])){$sport1="sport1";}
if(isset($_POST['sport2'])){$sport2="sport2";}
if(isset($_POST['sport3'])){$sport3="sport3";}
$email_message .= "Sports: ".clean_string($_POST["sport1"])."\n"
.clean_string($_POST["sport2"])."\n"
.clean_string($_POST["sport3"])."\n";
There are much tidier ways of doing it, but that should help you work it out?
Alternatively you could give each of the checkboxes the same name, and then construct an array out of them. This array can then be looped in the PHP email handler. Look at this SO answer for a great example: https://stackoverflow.com/a/4516887/3112128
I have following structure for check-boxes
<input type="checkbox" name="reg_field['first_name']" value="1" /> First Name<br />
<input type="checkbox" name="reg_field['last_name']" value="1" /> Last Name<br />
<input type="checkbox" name="reg_field['paypal_email']" value="1"/> PayPal Email<br />
<input type="checkbox" name="reg_field['website_url']" value="1" /> Website URL<br />
<input type="checkbox" name="reg_field['address']" value="1"/> Address<br />
For some installation, I can access these fields as
$reg_new_values = (isset($_POST['reg_field'])?$_POST['reg_field']:array());
echo $reg_new_values["'first_name'"]
but for some installation, it won't work at all?
Is my structure wrong, or do I have to change some settings in PHP.INI
Change the name to something like this reg_field[first_name] instead of reg_field['first_name']. Remove the single quotes, by doing that you can access each field name as:
$FirstName = $_POST['reg_field']['first_name'];