...

View Full Version : building form names & values with php



rowantrimmer
02-15-2010, 03:54 PM
Hello,

I have created a mysql table that contains the following fields
id
recipe_id
quanity
ingredient

I have sucessfully written a php script where upto 30 records can be populated into this table.

What I am struggling with is I need to allow the user to edit the recipe record

the input fields are <input type=text name=QTY[1]><input type=text name=ING[1]>

Where this could be anything between 1 and 30 sets of these tags depending on how many pairs the user entered data into.

I am guessing that I need to get the records from the table as follows...

and I only need to display the pairs of fields where the quantity field is NOT blank.

$viewmemberssql3 = "SELECT * FROM recipe_item WHERE recipe_id='$id' order by id asc";
$viewmembersquery3 = @mysql_query($viewmemberssql3,$connection) or die(mysql_error());
while ($viewmembersrow3 = mysql_fetch_array($viewmembersquery3))

{

if ($viewmembersrow3['quantity']!="")

{


print $viewmembersrow3['quantity']; print "<br>"; print $viewmembersrow3['ingredient']; print "<br>"; print $viewmembersrow3['id']; print "<br>";
}

}

This code seems to be OK but I am struggling to automatically build the tags for the form so that the user can edit them?

I am sorry if I haven't made myself very clear???????

masterofollies
02-15-2010, 04:15 PM
In the function, put the variables to edit. Such as


function edit($id) {

rowantrimmer
02-15-2010, 04:20 PM
Hello,

Thanks I seem to have it cracked now....

Could I please ask you this question.

Whilst pulling the records out of the table I increment var x in php by saying x++;

I could then do with using that counter in my javascript function that them immediatly follows with a for loop as follows...

<script type="text/javascript">
for ( var i = 2; i <= 30; ++i )
{
document.write( '<tr id="L' + i + '">'
+ '<td><input name="QTY[' + i + ']" onchange="showNextLine(this);" /></td>'
+ '<td><input name="ING[' + i + ']" onchange="showNextLine(this);" /></td>'


+ '</tr>');
}
</script>

What I need to do is to replace the highlighted red number to with the x php variable but when I simply replace the 2 with x I get a javascript error to say that x is not defined?

I suppose my question is how to I pass a variable from php to javascript?

I hope you can help.

Regards


Rowan

ahallicks
02-15-2010, 04:55 PM
You can't, basically. What you can do is store the value of x somewhere on your page, such as in a hidden input:



<input type="hidden" name="x-val" id="x-val" value="<?php echo $x; ?>" />

Then in javascript:



var i = document.getElementById('x-val').value;

Spudhead
02-15-2010, 05:27 PM
Can't you just do:


for ( var i = <?php echo $x; ?>; i <= 30; ++i )

?

rowantrimmer
02-15-2010, 07:26 PM
great idea spudhead, I will give that a go!

JAY6390
02-15-2010, 09:12 PM
You could also just echo out that javascript loop using php instead. Theres no reason why php can't do that for you



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum