note 50386 added to faq.html
| From: | richard dot prangnell at ntlworld dot com | Date: | Sat, 26 Feb 2005 16:44:39 +0000 |
| Subject: | note 50386 added to faq.html | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85583@lists.php.net to get a copy of this message | ||
The following HTML form page and PHP action page illustrate an elegant and powerful client side
JavaScript to server side PHP data transfer technique perfectly matched to PHP's associative
array feature. Browse to the form page and select some items by entering an INTEGER > 1 then
click on Submit (Don't put strings or reals in the input fields - error trapping has been
stripped out for brevity). Try selecting one, some, all or none of the items, changing the names of
the form input variables (and the associated labels, for clarity!) or adding to the number of items
"on offer". At no point do you need to alter either the JavaScript function or the action
page in any way - just be aware that extra coding and error trapping is required - especially where
the key values in the application could be something other than integers:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>How to Pass an Array from Javascript to PHP: Form Page</title>
<!-- File: random_picker.php -->
<script type="text/javascript">
function send_picks()
{
form1.picks.value="";
var picked = 0;
for (var i = 0 ; i < form1.elements.length; i++)
{
if(form1.elements[i].value > 0)
{
e = form1.elements[i]
picked++;
form1.picks.value = form1.picks.value + "s:" + e.name.length +
":\""
+ e.name + "\";i:" + e.value + ";";
}
}
form1.picks.value = "a:" + picked + ":{" + form1.picks.value +
"}";
}
</script>
</head>
<body>
<p>
<h3 align="center">How to Pass an Array from Javascript to PHP: Form Page</h3>
<form name="form1" action="random_picker_action.php" method="post"
onsubmit="JavaScript:send_picks()">
<table align="center" border="1" cellpadding="5"
cellspacing="0">
<tr><td>Item 13</td>
<td><input type="text" name="item_13" value="0"
size="4"></td>
</tr>
<tr><td>Part number 2</td>
<td><input type="text" name="Part_number_2" value="0"
size="4"></td>
</tr>
<tr><td>Drawing ref 327</td>
<td><input type="text" name="Drawing_ref_327" value="0"
size="4"></td>
</tr>
<tr><td>Organic Carrots</td>
<td><input type="text" name="Organic_carrots" value="0"
size="4"></td>
</tr>
<tr><td colspan="2" align="center">
<input type="submit" value="Submit Picks">
<input type="hidden" name="picks" value=""></td>
</tr>
</table>
</form>
</body>
</html>
The action page for the above form page is listed below. Notice the really ingenious feature; the
form variable is simply unserialized to turn it into a fully functional PHP array! The action page
doesn't need to know anything about "expected" variable names because everything PHP
needs to know is right inside the array. Note also that for maximum efficiency only
"picked" item data is sent. If no items at all are picked, a perfectly formed empty array
is sent:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>How to Pass an Array from Javascript to PHP: Action Page</title>
<!-- File: random_picker_action.php -->
</head>
<body>
<p>
<h3>How to Pass an Array from Javascript to PHP: Action Page</h3>
<?php
$picks = urldecode(stripslashes($picks));
echo "<b>JavaScript said: " . $picks . "</b><p>";
$picks = unserialize($picks);
if(sizeof($picks) > 0)
{
reset($picks);
echo "You picked the following items:<p>";
while($item = each($picks))
echo $item['value'] . " off, " . str_replace("_", "
", $item['key']) . "<br>";
}
else
echo "You did not pick any items<p>";
?>
</body>
</html>
I hope you find this technique useful - let me know what you think - I can already hear the jaws of
Java Jocks dropping!
----
Manual Page -- http://www.php.net/manual/en/faq.html.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50386
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50386&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50386&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50386&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50386&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50386&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50386&report=yes
Search -- http://master.php.net/manage/user-notes.php