Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog. You can find information on programming, linux, documentation, tips for code and database optimization, my thoughts and rants, and whatever else I feel like sharing. Feel free to contribute to the blog by posting comments and asking questions.
Programming

form checkboxes in php array and storing variables in url string

I picked up one or two new things in PHP this past week. One neat thing I learned is how to pass variables in a url string and how to retrieve them on the next page. There are lots of ways to pass variables between two pages in PHP, this one seems to have the least overhead, but makes for an ugly url. The other thing I learned is how to store checkbox values in an array using the foreach loop. I show examples of both if you’re interested below.

Passing variables in a url string

<'a href=�mysite.php?var1=x&var2=y�>click this link<'/a>

On the next page you retrieve these variables like this

$var1 = $_GET[“var1�];
$var2 = $_GET[“var2�];

Now this is much more simplistic then how I am actually using them. My code involves returning information stored in a mysql database, inserting them into the link tag, and then opening a popup window via javascript. Here’s an idea of what that looks like:

echo "<'a href=\"javascript:popUp('myupdate.php?id=". $row['id'] ."&uid=". $uid ."')\">Update<'/a>";

Putting checkbox values into an array

The html looks like this:


<'input type=�checkbox� name=�cars[]� value=�toyota�>
<'input type=�checkbox� name=�cars[]� value=�ford�>
<'input type=�checkbox� name=�cars[]� value=�mr bean car�>

On submission of the form you get the checked items with this code:

$cars = $_POST[“cars�];

foreach($cars as $c)
{
echo $c�;
}

No related posts.

Leave a Reply