i already saw other pages about this topic, but i can't find the solution for my case.
I have a form with almost 70 fields, and 3 of them are Checkbox where the user can choose more than one option and i need to save them all.
I know that probably thats an easy way to resolve this but i'm about 8 hours straight and i can't see nothing at this point...
HTML Form:
(...)
<p><b>Quais as empresas que reconhece como patrocinadores desta edição da prova?</b></p>
<input type="checkbox" name="Patrocinadores_Prova" value="EDP"><span style="padding-left:10px"></span>EDP<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Agência Abreu"><span style="padding-left:10px"></span>Agência Abreu<br>
<input type="checkbox" name="Patrocinadores_Prova" value="DietSport"><span style="padding-left:10px"></span>DietSport<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Dolce Vita Douro"><span style="padding-left:10px"></span>Dolce Vita Douro<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Europcar"><span style="padding-left:10px"></span>Europcar<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Fruut"><span style="padding-left:10px"></span>Fruut<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Jumbo"><span style="padding-left:10px"></span>Jumbo<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Liberty Seguros"><span style="padding-left:10px"></span>Liberty Seguros<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Reccua"><span style="padding-left:10px"></span>Reccua<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Vindimar"><span style="padding-left:10px"></span>Vindimar<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Vitalis"><span style="padding-left:10px"></span>Vitalis<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Wall Street English"><span style="padding-left:10px"></span>Wall Street English<br><br>
(...)
PHP Code:
(...)
$Patrocinadores_Prova = $_POST['Patrocinadores_Prova'];
(...)
mysql_query ("INSERT INTO questionario(...'" . implode(',', $Patrocinadores_Prova) ."'....)
if you want to save multiple check boxes value in database you have to use array[], so first change your chackboxes name from name="Patrocinadores_Prova" to name="Patrocinadores_Prova[]" and then execute your php code.
Related
All the inputs have the same name as option<?=$x?> for each loop but always the input with type=hidden of the last line overrides all of them although one of the radio buttons with same name option<?=$x?> is selected
Is there anyway so that I can check whether anyone out of four radios of above is chosen or not, if not then I have to pass the last input instead of them
<input type="radio" name="option<?=$x?>" value="<?=$chioce[0].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[0]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[1].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[1]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[2].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[2]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[3].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[3]?>
<input type="hidden" name="option<?=$x?>" value="null;<?=$data['id'].';'.$data['subject']?>">
You can try this
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[0].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[0]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[1].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[1]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[2].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[2]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[3].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[3]?>
<input type="hidden" name="option[<?=$x?>]['hidden']" value="null;<?=$data['id'].';'.$data['subject']?>">`
Now when you post you will get option array
$options = $_POST['option'];
$radioValue = $options[$x]['radio'];
$hiddenValue = $options[$x]['hidden'];
You can read value like this
$optionValue = isset($options[$x]['radio']) ? $options[$x]['radio'] : $options[$x]['hidden'];
Hope it may help you
I have looked at this post PHP, pass array through POST
but cannot quite get it working.
I have a form as below:
<applications><h2>Applications</h2><br><input type="checkbox" name="apps[]" value="gg">gg<br><br>
<input type="checkbox" name="apps[]" value="aa">aa<br><br>
<input type="checkbox" name="apps[]" value="bb">bb<br><br>
<input type="checkbox" name="apps[]" value="cc">cc<br><br>
<input type="checkbox" name="apps[]" value="dd">dd<br><br>
</applications>
<servers>
<h2>Servers</h2><br><input type="checkbox" name="serv[]" value="servera">servera<br><br>
<input type="checkbox" name="serv[]" value="serverb">serverb<br><br>
</servers>
<countries1><h2>Countries</h2><br><input type="checkbox" name="country" value="uk">UK<br><br>
<input type="checkbox" name="country[]" value="germany">Germany<br><br>
<input type="checkbox" name="country[]" value="france">France<br><br>
</countries1>
<countries2>
<input type="checkbox" name="country[]" value="spain">Spain<br><br>
<input type="checkbox" name="country[]" value="sweeden">Sweeden<br><br>
</countries2>
<submitb>
<?
session_start();
$_SESSION['country']=$country;
$_SESSION['serv']=$serv;
$_SESSION['apps']=$apps;?>
<input type="submit" value="Update">
</submitb>
</form>
Then on my retrieval end:
$apps=$_SESSION['apps'];
$countries=$_SESSION['country'];
$servers= $_SESSION['serv'];
EDIT:Please could someone advise me what I am doing wrong here? I am receiving:
Undefined index: apps in C:\wamp\www\notifcation system\done.php on line 41
<?php
$apps=$_SESSION['apps']; /* fix missing semicolon */
$countries=$_SESSION['country'];
$servers= $_SESSION['serv'];
?>
For your other issue:
Undefined index: apps in ...
Simply means that $apps is not defined. This is not an error, but a notice.
Try this
if(!isset($_SESSION['apps'])){
$apps=$_SESSION['apps'];
} // etc
Note that session_start(); should always be on the first line of the page, then you will start adding things later.
Also the session_start(); should be on every page you need acces to your session to.
as mentioned above by #subzero, never forget the all important ; semicolons as in PHP, these things can break your code.
I have these 4 radio buttons in which i am submitting to a validatepreferences.php which is the php code below however i am struggling to understand why when i click submit nothing is going through the if statement therefore not giving me my cookie in which to change images based on user input
<input type="radio" name="radioimage"><img class="prefimage" src="../images/image1.jpg">
<br>
<input type="radio" name="radioimage"><img class="prefimage" src="../images/image2.jpg">
<br>
<input type="radio" name="radioimage"><img class="prefimage" src="../images/image3.jpg">
<br>
<input type="radio" name="radioimage"> No Picture
I think the php code must have an error or my if is not right altough i cannot see it.
<?php
if(isset($_POST['radioimage'])){
$radioimage = $_POST['radioimage'];
if ($radioimage == "0" || $radioimage == "1" || $radioimage == "2" || $radioimage =="3") {
setcookie("image", $radioimage, time()+300);
}
}
?>
You're not giving the radiobuttons values in the form. You have to give them values so you can retrieve those values with $_POST in validatepreferences.php. So the HTML should be:
<input type="radio" name="radioimage" value="1"><img class="prefimage" src="../images/image1.jpg">
<br>
<input type="radio" name="radioimage" value="2"><img class="prefimage" src="../images/image2.jpg">
<br>
<input type="radio" name="radioimage" value="3"><img class="prefimage" src="../images/image3.jpg">
<br>
<input type="radio" name="radioimage" value="4"> No Picture
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Adding multiple html checkboxes with the same name to URL using $_GET
I have the following code:
<script language="JavaScript">
function toggle(source) {
checkboxes = document.getElementsByName('foo[]');
for(var i in checkboxes)
checkboxes[i].checked = source.checked;
}
</script>
<input type="checkbox" onClick="toggle(this)" /> Toggle All<br/>
<input type="checkbox" name="foo[]" value="bar1"> Bar 1<br/>
<input type="checkbox" name="foo[]" value="bar2"> Bar 2<br/>
<input type="checkbox" name="foo[]" value="bar3"> Bar 3<br/>
<input type="checkbox" name="foo[]" value="bar4"> Bar 4<br/>
I just want to ask how will I able to get all the values on the checkboxes that are checked using PHP?
I'm not sure I'm understanding you but here's my impression:
You need to use an input array:
<input type="checkbox" name="foo[]" value="bar1"> Bar 1<br/>
<input type="checkbox" name="foo[]" value="bar2"> Bar 2<br/>
<input type="checkbox" name="foo[]" value="bar3"> Bar 3<br/>
<input type="checkbox" name="foo[]" value="bar4"> Bar 4<br/>
Otherwise you're just assigning the last occurrence that is valid as the posted input.
If you want to process multiple boxes give each one a name ending with "[]" to create an array.
Only the values of the checked boxes will be sent to the php script:
// $_REQUEST['foo'] will return an array of the values
echo join(',', $_REQUEST['foo']);
I have built a form that has a checkbox input array (saving to an array). However, when I POST it and retrieve the results, it only offers the last selection.
<input type="checkbox" value="Friendly" name="quest[9]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[9]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[9]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[9]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[9]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[9]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>
For example, say I chose "Friendly", "Efficient", and "Genuine".
When I POST it over to a PHP document and run
print_r($_POST['quest']);
I only receive
Array ( [9] => Genuine )
back so "Genuine" is the only item I get back. Is there a way to fix this? What have I done wrong?
This is the 9th question on the survey, so changing the name unfortunately is not an option. Is there any way to combine the results to that single array separated by commas? I could always explode on the php side.
All your checkboxes have the same name, make them unique
<input type="checkbox" value="Friendly" name="quest[3]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[4]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[5]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[6]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[7]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[8]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>
or use empty square brackets so php will treat the inputs as an array
<input type="checkbox" value="Friendly" name="quest[]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[]"> Genuine<br>
use quest[] in name instead of quest[9].
also in php part use this to add multiple choices .
<?php
$quest = implode(',',$_post['quest']);
print_r($quest);
?>
Happy coding!!
I'm posting a new answer about your comments on the previous one:
Since you must keep quest[9] as the organization for the checkbox array..
You may want to try and make it a more complex array, where each <input> has name="quest[9][1]", name="quest[9][2]" and so on.
And find the contents by
print_r($_POST['quest']);
again