简体   繁体   中英

sending an array using a form and sessions (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM