PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Sat Mar 10, 2007 1:52 am
Hi All,
Is there an elegant way to these dom/html values into a php array to add them to a database?
So far I have only managed to echo them all. There is likely to be an unditermind amount of rows.
I know how to do this if PHP was creating the HTML code but not with DOM creating it.
Any pointers or ideas?
ta, Will./
Code: Select all
if(isset($_POST['commented'])) {
// Get all posted variables!
foreach ($_POST as $var => $value) {
$var = ereg_replace("[^A-Za-z]", "", $var);
echo "$var = $value<br>\n";
}
for ($i = 0; $i < "driverType"; $i++) {
echo "Yup";
}
} else {
?>
<script type="text/javascript"> <!--//
var TDCount = 0;
function makeRow() {
TDCount++;
mypara=document.getElementById("paraID");
myselect = document.createElement("select");
myselect.setAttribute("name","driverType" + (TDCount));
myinput = document.createElement("input");
myinput.setAttribute("name","rowcount" + (TDCount));
myinput.setAttribute("value","rowcount" + (TDCount));
myinput.setAttribute("type","hidden");
//first option
theOption=document.createElement("option");
theText=document.createTextNode("--Select--");
theOption.appendChild(theText);
myselect.appendChild(theOption);
// First option
theOption=document.createElement("option");
theText=document.createTextNode("Deliver");
theOption.appendChild(theText);
theOption.setAttribute("value","delivery");
myselect.appendChild(theOption);
// Second option
theOption=document.createElement("option");
theText=document.createTextNode("Return");
theOption.appendChild(theText);
theOption.setAttribute("value","return");
myselect.appendChild(theOption);
//now the select has the options added with their text, values
mypara.appendChild(myselect);
mypara.appendChild(myinput);
} //-->
</script>
<?php
echo "<body onload=\"makeRow()\">";
echo "<form id=\"driverSummary\" name=\"driverSummary\" method=\"POST\" action=\"".$_SERVER['PHP_SELF']."\">\n";
echo "<input type=\"hidden\" name=\"commented\" value=\"set\">\n";
echo "<table>";
echo "<tr><td valign=bottom><div id=\"paraID\"></div></td></tr>\n";
echo "</table>";
echo "<br><br><br><input type=\"button\" value=\"Create Form\" onclick=\"makeRow()\" /><input type='reset' class='btn' value='Reset'><input type='submit' class='btn' value='Submit'></form>";
echo "</form>";
}
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Mar 10, 2007 2:16 am
You do it exactly the same way. It doesn't matter wether it's a static html file or html code generated by php or client-side dom.
When the form is submitted the browser sends the name/value-pairs of all
successful form controls via GET or POST to the server.
Your form uses POST, so take a look at the $_POST
Code: Select all
<?php echo '<pre>'; var_export($_POST); echo "</pre>\n";
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Sat Mar 10, 2007 2:26 am
Thanks for the reply! I probably wasn't explicate enough.
The output from the array is :
Code: Select all
array (
'commented' => 'set',
'driverType1' => 'delivery',
'rowcount1' => 'rowcount1',
'driverType2' => 'delivery',
'rowcount2' => 'rowcount2',
'driverType3' => 'return',
'rowcount3' => 'rowcount3',
)
and my code loops through using a count. How can I set count = "amount of rowcount1' => 'rowcount1'' ?
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Mar 10, 2007 2:46 am
you can test wether there is such an element in each iteration and stop if there isn't.
Code: Select all
$i=1;
while( isset($_POST['driverType'.$i]) ) {
echo $_POST['driverType'.$i], "<br />\n";
$i++;
}personally I like appending [] to the element name and then using a foreach loop better.
Code: Select all
<html>
<head>
<title>...</title>
</head>
<body>
<?php
if (isset($_POST['driverType']) && is_array($_POST['driverType'])) {
foreach($_POST['driverType'] as $dt) {
echo $dt, "<br />\n";
}
}
?>
<form method="post">
<select name="driverType[]">
<option>a</option><option>b</option><option>c</option>
</select>
<select name="driverType[]">
<option>a</option><option>b</option><option>c</option>
</select>
<select name="driverType[]">
<option>a</option><option>b</option><option>c</option>
</select>
<input type="submit" />
</form>
</body>
</html>
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Sat Mar 10, 2007 3:10 am
that's sweet!
thanks volka.
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Sat Mar 10, 2007 7:18 am
Hi Again,
How could I write this for use with DOM?
I understand DOM is clientside so it can't directly interact with a database. I have looked into xml and json but haven't found any working examples I could learn from.
Does anyone have any pointers?
Code: Select all
echo "<td width=150px>\n";
$sql_query = mysql_query("SELECT id, prod_name from product ORDER BY prod_name ASC");
echo "<select name=\"productId[$section]\">\n";
echo "<option value=\"\">-- Please Select --</option>\n";
while(list($id, $prod_name)=mysql_fetch_array($sql_query)) {
$prod_name = stripslashes($prod_name);
echo "<option value=\"$id\">$prod_name</option>\n";
}
echo "</select>";
mysql_free_result($sql_query);
echo "</td>\n";
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Mar 10, 2007 7:23 am
As is, it can be used with the DOM (Javascript.)
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Sat Mar 10, 2007 7:36 am
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Any chance of a little more information?
My DOM code is looking a little like this..
[syntax="javascript"]
myProduct = document.createElement("select");
myProduct.setAttribute("name","productId[" + (TDCount) + "]");
//Product Setup Option
theProduct=document.createElement("option");
theText=document.createTextNode("-- Please Select --");
theProduct.appendChild(theText);
myselect.appendChild(theProduct);
//Product Setup Option
theProduct=document.createElement("option");
theText=document.createTextNode("$prod_name");
theProduct.appendChild(theText);
theOption.setAttribute("value","$id");
myProduct.appendChild(theProduct);
feyd | Please use[/syntax]Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Mar 10, 2007 7:44 am
Is that not working?
facets
Forum Contributor
Posts: 273 Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit
Post
by facets » Sat Mar 10, 2007 7:52 am
Nah, it's not working. Here's the latest.
It's displaying $prod_name rather than the actual Product Name.
Code: Select all
//#--------------------------------------
//# Products
//#--------------------------------------
myProduct = document.createElement("select");
myProduct.setAttribute("name","productId[" + (TDCount) + "]");
//Product Setup Option
theProduct=document.createElement("option");
theText1=document.createTextNode("-- Please Select --");
theProduct.appendChild(theText1);
myselect.appendChild(theProduct);
';
$sql_query = mysql_query("SELECT id, prod_name from product ORDER BY prod_name ASC");
while(list($id, $prod_name)=mysql_fetch_array($sql_query)) {
$prod_name = stripslashes($prod_name);
print '
theProduct=document.createElement("option");
theText1=document.createTextNode("$prod_name");
theProduct.appendChild(theText1);
theOption.setAttribute("value","$id");
myProduct.appendChild(theProduct);
';
}
mysql_free_result($sql_query);
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Mar 10, 2007 7:53 am
Your print is a single quote string. Variables referenced will not be parsed.