sending an array using a form and sessions (PHP) - php

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.

Related

Combine form values under one name using GET method

I have a form using the GET method consisting of checkboxes.
This form is sending data to another page that is receiving the GET info and using it to pull info from a json api. I need to have it send the name once with all values combined into one string like this: example.com/color=RedGreenBlue
I am able to get all values combined and echoed onto the page but because they are in a foreach loop I am not able to pass them in the form. I tried below using the hidden field to pass them with no luck.
This is the method I've seen suggested but does not work for me:
<form action="" method="get">
Red<input type="checkbox" name="color[]" value="Red">
Green<input type="checkbox" name="color[]" value="Green">
Blue<input type="checkbox" name="color[]" value="blue">
<input type="submit" value="submit">
<?php
$name = $_GET['color'];
if (isset($_GET['color'])) {
foreach ($name as $color){
echo $color;
}
}
?>
<input type="hidden" name="MajorArea" value="<?php echo $color; ?>" />
</form>
Is there a way to assign one name to a group of checkboxes? Is there a way to pull the foreach loop data and use it outside a loop? Am I overlooking a way that is much easier than this?
Thanks for any advice!
I'll go out on a limb here and delete if it isn't what you're after. $_GET['color'] will be an array or it will be empty. You could also use isset:
<form action="" method="get">
Red<input type="checkbox" name="color[]" value="Red">
Green<input type="checkbox" name="color[]" value="Green">
Blue<input type="checkbox" name="color[]" value="blue">
<input type="submit" value="submit">
</form>
<?php
if (!empty($_GET['color'])) {
echo implode($_GET['color']);
}
?>

Save Multiple Checkbox into MySQL with PHP

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.

checkbox array only returning one result

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

Getting ID's from checkboxes

I'm trying to get the ID's from a load of checkboxes that are in rows that are printed out on a page using a while statement. Each row from the database has a checkbox next to it with the ID in the checkbox value.
Basically I want to do a update query on the checkbox-selected rows, using the ID.
The code for the checkboxes that I have used is:
<input type="checkbox" name="check_list[]" value="<? echo $rows['id']; ?>">
Then when the code for the submit is:
<?
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $id){
echo "$id was checked! ";
}
}
?>
Just wanted to echo out the results to test that it works before putting it into a query. Trouble is...nothing happens. I just get a blank screen. No error or anything. Surely it should work, it looks right but I don't understand why it doesnt work.
Any help is most appreciated! :)
Tested below code with one test.php file
<?php
if(!empty($_POST['check_list']))
{
foreach($_POST['check_list'] as $id){
echo "<br>$id was checked! ";
}
}
?>
<form method="post" name="frm">
<input type="checkbox" name="check_list[]" value="1"> 1
<input type="checkbox" name="check_list[]" value="2"> 2
<input type="checkbox" name="check_list[]" value="3"> 3
<input type="checkbox" name="check_list[]" value="4"> 4
<input type="submit" name="submit" />
</form>
please check if you are getting $rows['id'] properly. Things should work fine otherwise.
Thanks.

PHP post checkbox problem

i have a form that utilizes checkboxes.
<input type="checkbox" name="check[]" value="notsure"> Not Sure, Please help me determine <br />
<input type="checkbox" name="check[]" value="keyboard"> Keyboard <br />
<input type="checkbox" name="check[]" value="touchscreen"> Touch Screen Monitors <br />
<input type="checkbox" name="check[]" value="scales">Scales <br />
<input type="checkbox" name="check[]" value="wireless">Wireless Devices <br />
And here is the code that process this form in a external php file.
$addequip = implode(', ', $_POST['check']);
I keep getting this error below;
<b>Warning</b>: implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>.../process.php</b> on line <b>53</b><br />
OK
is any of your checkboxes ticked? php’s $_POST array will only have checkboxes which have been ticked
to silence your warning use this:
$addequip = implode(', ', empty($_POST['check']) ? array() : $_POST['check'] );
the following site seems to be what you need:
http://www.ozzu.com/programming-forum/desperate-gettin-checkbox-values-post-php-t28756.html
Hi I m the original user who posted this question i couldnt login to my account so posting from another account. After couple hours of trying i somehow managed to make it work partially. Below is the modified form html and process code for checkboxes
<input type="checkbox" name="check" value="Touchscreen"> Touchscreen<br>
<input type="checkbox" name="check" value="Keyboard"> Keyboard<br>
<input type="checkbox" name="check" value="Scales"> Scales<br>
I had to remove the [] so it would work. Also below is the entire post method for those would like to see. It works perfectly fine with every other field.
<form id="contact_form" action="process.php" method="POST" onSubmit="return processForm()">
And below is the php code to process checkboxes. For some reason i have to tell script that $_POST['check'] is an array without it would only return array. All other methods suggested returns invalid argument passed error.
$chckbox = array($_POST['check']);
if(is_array($chckbox))
{
foreach($chckbox as $addequip) {
$chckbox .="$addequip\n";
}
}
So this code works but returns only 1 checkbox value that is ticked even no matter how many you ticked.

Categories