Page 1 of 1

Form variables into a PHP array from DOM

Posted: Sat Mar 10, 2007 1:52 am
by facets
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>";
}

Posted: Sat Mar 10, 2007 2:16 am
by volka
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";

Posted: Sat Mar 10, 2007 2:26 am
by facets
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'' ?

Posted: Sat Mar 10, 2007 2:46 am
by volka
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>

Posted: Sat Mar 10, 2007 3:10 am
by facets
that's sweet!
thanks volka.

Posted: Sat Mar 10, 2007 7:18 am
by facets
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";

Posted: Sat Mar 10, 2007 7:23 am
by feyd
As is, it can be used with the DOM (Javascript.)

Posted: Sat Mar 10, 2007 7:36 am
by facets
feyd | Please use

Code: Select all

,

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

,

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]

Posted: Sat Mar 10, 2007 7:44 am
by feyd
Is that not working?

Posted: Sat Mar 10, 2007 7:52 am
by facets
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);

Posted: Sat Mar 10, 2007 7:53 am
by feyd
Your print is a single quote string. Variables referenced will not be parsed.