I've got a system where I allow a user to select multiple checkboxes from n amount of checkboxes, but also want two more inputs associated with each checkbox. This is for a message, a date and a time. When I post the data to be processed by a PHP script, I'd like to be able to access each of the sets of checkbox and two other inputs so I can see what date and time a user has filled in for each of the messages they've selected. I'm having trouble coming up with a method to associate the two other inputs with each checkbox.
Any ideas how to do this?
You can use arrays in your html inputs like so...
<input type="text" name="messages[1][message]" value="herp" />
<input type="text" name="messages[1][date]" value="24th April" />
<input type="text" name="messages[1][time]" value="13:00" />
<input type="text" name="messages[2][message]" value="derp" />
<input type="text" name="messages[2][date]" value="26th April" />
<input type="text" name="messages[2][time]" value="18:00" />
<?php
$messages = $_REQUEST['messages'];
foreach ($messages as $messageId => $value){
echo $value['message'];
echo $value['date'];
echo $value['time'];
}
Your HTML:
<input type="checkbox" name="car" />
<input type="text" name="msg_car" value="Car message" />
<input type="text" name="date_car" value="Car date" />
<input type="checkbox" name="bike" />
<input type="text" name="msg_bike" value="Bike message" />
<input type="text" name="date_bike" value="Bike date" />
<input type="checkbox" name="train" />
<input type="text" name="msg_train" value="Train message" />
<input type="text" name="date_train" value="Train date" />
<input type="checkbox" name="plane" />
<input type="text" name="msg_plane" value="Plane message" />
<input type="text" name="date_plane" value="Plane date" />
Your PHP script:
$array = array("car", "bike", "train", "plane");
for ($i = 0; $i < count($array); $i++) {
if (isset($_POST[$array[$i]])) {
//Checkbox was checked, get values
$msg = "";
$date = "";
$msg_id = "msg_" . $array[$i];
$date_id = "date_" . $array[$i];
if (isset($_POST[$msg_id]))
$msg = $_POST[$msg_id];
if (isset($_POST[$date_id]))
$date = $_POST[$date_id];
}
}
I think something like this should work. I didn't test it.. So forgive me if this example still contains some minor errors.
Related
I have an HTML form that looks like this:
<form method="POST" action="ideal_test.php">
<input type="text" name="member[email]" placeholder="email" /><br />
<input type="text" name="member[country_code]" placeholder="country code" /><br />
<input type="text" name="member[first_name]" placeholder="first name" /><br />
<input type="text" name="member[last_name]" placeholder="last name" /><br />
<input type="text" name="member[street]" placeholder="street" /><br />
<input type="text" name="member[house_nr]" placeholder="house number" /><br />
<input type="text" name="member[houseNrExtension]" placeholder="house number addition" /><br />
<input type="text" name="member[postcode]" placeholder="postcode" /><br />
<input type="text" name="member[city]" placeholder="city" /><br />
<input type="text" name="member[phone]" placeholder="phone number" /><br />
<input type="text" name="member[birthday]" placeholder="birthday" /><br />
<input type="text" name="member[gender]" placeholder="gender" /><br />
<input type="text" name="member[ideal_bank]" placeholder="iDeal bank" /><br />
<input type="hidden" name="payment" value="ideal" />
<input type="submit" value="Test iDeal API call" />
The reason the form has been made this way is because I use an API that has to receive the data in this format. When I change the form action to the API URL, it works, however, sometimes I have to handle the form data myself with my own PHP and I don't know how I should process this form with PHP.
This is what I've tried:
$data = $_POST[ 'member' ];
foreach( $data AS $row ) {
echo $row . '<br />';
}
This shows all the data nicely separated by the <br />. However, I need to access each input separately, so I've tried this to achieve that:
foreach( $data AS $row ) {
echo $row['email'] . '<br />';
}
When I do this, it displays the first letter of each input, also separated by <br />. How can I loop through the member[] array and get every input separately?
$data is an array. So if you want to access each input separately You don't need to do it in foreach loop. You simply print it as normal array variable. Like this:
<?php
echo $data['email'];
echo $data['first_name'];
// Like this.
?>
Here i my form i am having four checkboxes with each having a textbox for its description
<form action="" method="post">
<input type="checkbox" name="days[]" value="Sunday"/>Sunday
<input type="text" name="description[]" /><br/>
<input type="checkbox" name="days[]" value="Monday"/>Monday
<input type="text" name="description[]" /><br/>
<input type="checkbox" name="days[]" value="Tuesday"/>Tuesday
<input type="text" name="description[]" /><br/>
<input type="checkbox" name="days[]" value="Wednesday"/>Wednesday
<input type="text" name="description[]" />
<input type="submit" name="submit" value="submit">
</form>
Here the user can choose any textbox and can write its description...now at the time of insertion...i want to insert only those values where checkboxes are checked...
For e.g: If i choose monday and tuesday then monday and tuesday should be inserted along with their respectice textboxes....my problem is that when i am submitting then the checkbox values are going right but only single description is getting inserted....here is mmy php script...
if(isset($_POST["submit"]))
{
for($i=0;$i<count($_POST["days"]);$i++)
{
$dayz=$_POST["days"][$i];
$description=$_POST["description"][$i];
mysql_query("insert into transport_two (transport_id,name) values ('$dayz','$description')");
}
}
?>
Can anyone help??
First of all, why aren't you making your life easier; and giving them a normal non array name?
What you can do is;
if ((isset($_POST["days"]) && isset($_POST["description"])) {
$days = $_POST["days"];
$desc = $_POST["description"];
$len = count($days);
for ($i = 0; $i <= $len-1; $i++) {
$noID = filter_var($days[$i], FILTER_SANITIZE_NUMBER_INT);
echo $days[$noID] . " " . $desc[$noID];
}
}
Check if that will work for you :)
Tested & Working:
HTML
<input type="checkbox" name="days[0][day]" value="Sunday"/>Sunday <input type="text" name="days[0][value]" />
<br/><input type="checkbox" name="days[1][day]" value="Monday"/>Monday <input type="text" name="days[1][value]" />
<br/><input type="checkbox" name="days[2][day]" value="Tuesday"/>Tuesday <input type="text" name="days[2][value]" />
<br/><input type="checkbox" name="days[3][day]" value="Wednesday"/>Wednesday <input type="text" name="days[3][value]" />
PHP
<?PHP
$arr = $_POST["days"];
for ($i = 0; $i < count($arr); $i++) {
if (!empty($arr[$i]['day'])) {
echo $arr[$i]['day'] . " and.... its value is " . $arr[$i]['value'];
}
}
?>
In php
<?php
if (isset($_POST['chk_group'])) {
$dayArray = $_POST['days'];
$decArray =$_POST['description'];
for ($i=0; $i<count($dayArray); $i++) {
$dayz=$dayArray[$i];
$description=$decArray[$i];
mysql_query("insert into transport_two (transport_id,name) values ('$dayz','$description')");
}
}
?>
try to use description[dayname] in the form and use $description=$_POST["description"][$dayz] in processing, you have array indexing issue because checkbox are just not send if not checked.
here i my form i am having four checkboxes with each having a textbox for its description
<form action="" method="post">
<input type="checkbox" name="days[]" value="Sunday"/>Sunday <input type="text" name="description[Sunday]" />
<br/><input type="checkbox" name="days[]" value="Monday"/>Monday <input type="text" name="description[Monday]" />
<br/><input type="checkbox" name="days[]" value="Tuesday"/>Tuesday <input type="text" name="description[Tuesday ]" />
<br/><input type="checkbox" name="days[]" value="Wednesday"/>Wednesday <input type="text" name="description[Wednesday]" />
<input type="submit" name="submit" value="submit">
</form>
and php:
if(isset($_POST["submit"]))
{
for($i=0;$i<count($_POST["days"]);$i++)
{
$dayz=$_POST["days"][$i];
$description=$_POST["description"][$dayz];
mysql_query("insert into transport_two (transport_id,name) values ('$dayz','$description')");
}
}
this may help
$str = '';
for($i=0;$i<count($_POST["days"]);$i++)
{
$dayz=$_POST["days"][$i];
$description=$_POST["description"][$i];
$str .= "(".$dayz.", ".$description.")";
if ((count($_POST["days"])-1) < $i) {
$str .= ',';
}
}
mysql_query("insert into transport_two (transport_id,name) values ".$str) ;
Firstly name each text input and check-box with different name.
<form action="" method="post">
<input type="checkbox" name="days1" value="Sunday"/>Sunday
<input type="text" name="days1_1" /><br/>
<input type="checkbox" name="days2" value="Monday"/>Monday
<input type="text" name="days2_2" /><br/>
<input type="checkbox" name="days3" value="Tuesday"/>Tuesday
<input type="text" name="days3_3" /><br/>
<input type="checkbox" name="days4" value="Wednesday"/>Wednesday
<input type="text" name="days4_4" />
<input type="submit" name="submit" value="submit">
</form>
Use this code for checking which check-box are checked.
<?php
if(isset($_POST["submit"]))
{
for($i=0;$i<$number_of_days;$i++)
{
$dayz="days".$i;
$description=$dayz."_".$i;
if(isset($dayz))
{
mysql_query("insert into transport_two (transport_id,name) values ('$dayz','$description')");
}
}
}
?>
Sorry for grammar mistakes. I have multiple dynamically created text fields (more than 100). how can i get the values of these fields. i don't know the number of fields. Please help me.
Example
<input type="text" name="id_1" />
<input type="text" name="id_2" />
<input type="text" name="id_3" />
................
<input type="text" name="id_100" />
Use arrays:
<input type="text" name="id[1]" />
<input type="text" name="id[2]" />
<input type="text" name="id[3]" />
Then:
foreach($_POST['id'] as $key => $value) {
echo "text $key = $value";
}
If you mean you want to get their values in PHP easily, just name them like "name[]".
<form method="post" action="yourscript.php">
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="submit" value="Submit" />
</form>
This way you'll be able to get the values in yourscript.php. They'll be in $_POST['input']. Just loop over it using foreach to get the values:
foreach($_POST['input'] as $value) {
// do what you want with the $value
}
Having index numbers in the names like this:
<input type="text" name="input[1]" />
<input type="text" name="input[2]" />
is redundant.
In HTML forms, using jQuery:
$(function() {
$("input[name^='id']").each(function() {
var name = $(this).attr('name');
var inputValue = this.value;
console.log(name + " : "+ this.value);
});
});
Hoa about adding a hidden field to pass the number of fields to the processing script?
<input type='hidden' name='num_text_fields' valus='100>
The do a for loop to get the values
$num_text_fields = $_POST['num_text_fields'];
for ($i=1;$i<=$num_text_fields;$i++)
{
$text_field_name = "id_" . $i;
$value = $_POST[$text_field_name];
// do something with $valus
}
I think the best way to do it is to add a class element to your input.
Then you can be easily able to find them regardless of how many you have
I have had a look at a few answers but none relates to my specific problem. I have an array that takes user input and sorts the array in an ascending order. That's all good, I now want the user to enter a number for which I can locate the array index, if the number is not located, the function returns -1. I cannot put my finger on the error, I'm getting -1 at all times.
Any help is truly appreciated
This is my form that picks up the information.
<form action="welcome.php" method="POST" value="">
Name: <input type="text" name="fname" value="">
Age: <input type="text" name="age" value="">
Integer :<input type="number" name="integer" value="">
<div class="container">
<p> Please choose numbers: </p>
<label>First:</label>
<input type="number" name="name[]" ><br />
<label>2nd</label>
<input type="number" name="name[]"><br />
<label>3rd</label>
<input type="number" name="name[]" ><br />
<label>4th</label>
<input type="number" name="name[]"><br />
<label>5th</label>
<input type="number" name="name[]"><br />
<label>6th</label>
<input type="number" name="name[]"><br />
<label>7th</label>
<input type="number" name="name[]"><br />
<label>8th</label>
<input type="number" name="name[]"><br />
<label>9th</label>
<input type="number" name="name[]"><br />
<label>10th</label>
<input type="number" name="name[]"><br />
<label>Pos Number</label>
<input type="number" name="pos"><br />
<input type="submit">
And the index locator:
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.
<?php
$tmp = trim($_POST['integer']);
if (!ctype_digit($tmp)) {echo "Input requires an inetger " ;}
else{
echo "The integer is ".$_POST["integer"]."<br>";
}
echo"You have entered the numbers below <br>";
$name=$_POST['name'];
sort($name);
foreach( $name as $v) {
print $v."<br>";
}
echo "You wish to find the position of ".$_POST['pos']."<br />";
function search($name,$K){
$l=0;
$size=sizeof($name);
$u=$size-1;
$m=0;
while ($l<=$u){
$m = ($l+$u)/2;
round($m);
echo "this is m ".$m;
if ($name[$m]<$K) {
$l=$m+1;
}
else if ($name[$m]==$K) {
return $m;
return $m;
} else {
$u=$m-1;
}
}
return -1;
}
$po=$_POST['pos'];
$b=search($name,$po);
echo "The position of the entered number is".$b;
?>
First of all thanks in advance, this has been very frustrating and I'm hoping someone can see something I'm not, I am definitely no php expert. Well here' what is going on.
I have a form where I have a checkbox for people to opt in to our newletter. The form element looks like this:
<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />
That is then submitted to a php file with this code:
if (isset($_POST['newsletter']) && $_POST['newsletter'] == 'Yes'){
echo "newletter yes";
$newsletter = 1;
}else{
echo "newsletter no";
$newsletter = 0;
}
$newsletter is then inserted into a database field.
The issue is that whether the box is checked or not it is being sent to php as true, so every entry is receiving the newsletter.
Any help would be greatly appreciated! Thanks!
Here's the full form minus the option list for the sake of brevity
<form method="post" action="contact.php" name="contactform" id="contactform">
<fieldset>
<legend>Please fill in the following form all fields are required, thanks!</legend>
<label for=firstName accesskey=F><span class="required">*</span>First Name</label>
<input name="firstName" type="text" id="firstName" size="30" value="" />
<br />
<label for=lastName accesskey=L><span class="required">*</span>Last Name</label>
<input name="lastName" type="text" id="lastName" size="30" value="" />
<br />
<label for=email accesskey=E><span class="required">*</span>Email</label>
<input name="email" type="text" id="email" size="30" value="" />
<br />
<label for=city accesskey=C><span class="required">*</span>City</label>
<input name="city" type="text" id="city" size="30" value="" />
<br />
<label for=state accesskey=S><span class="required">*</span>State</label>
<select name="state" type="text" id="state">
<option value="AL">Alabama</option>
...
<option value="WY">Wyoming</option>
</select>
<br />
<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />
<br />
<p><span class="required">*</span> Are you human?</p>
<label for=verify accesskey=V> 3 + 1 =</label>
<input name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" /><br /><br />
<input type="submit" class="submit" id="submit" value="Submit" />
</fieldset>
</form>
Your code is correct. You most likely have a problem with your database insert/update logic. Why do you assume it is the PHP form handling?
This has just dawned on me.
If a checkbox is unchecked it isn't set in the $_POST superglobal. So if !isset($_POST['newsletter']) then it wasn't checked - if isset($_POST['newsletter']) it was checked.
Edit: Remove the 'yes' part - the value will never be yes, just true or 'on'.
Edit 2:
I've tested this to death. Change your code to:
if (isset($_POST['newsletter'])){
echo "newletter yes";
$newsletter = 1;
}else{
echo "newsletter no";
$newsletter = 0;
}
Remove the value="Yes" attribute from your checkbox input also. If you want it checking by default use checked="checked".