Jump to content

Forms in a loop.


tomas.hortlax

Recommended Posts

HiThanks for all help with my problems.This problem started with "Link Further Databas Results"That's working fine now. But I want to put some more functions in my loop.In each row I want three forms field. In the first form field I want publish a calculation of two databas values.Like a default value. And two empty forms field.After this two forms I want four small icon. (1) =submit the default value (or a new value) from the first forms. (2)=calculate a value from form 2 and publish it in form 3. (3)=calculate a value from form 3 and publish it in form 2. (4)=send the two values to databas.And this I want on each published databas row in my site.Where to begin?What to read?Proposal?Is this to hard to do for me? I'm new on this.(where is Basic, I miss you)/Tomas

Link to comment
Share on other sites

You'll want to use ajax for that, so it's best to get started learning ajax. When you're comfortable with how ajax works and the ideas behind it, then you can get started with that. Printing those things on the page shouldn't be difficult for you, you were looping through records in the other thread so you should at least know how to print all of the fields and icons out. The icons will need to use Javascript to do the calculations and send everything to the database though, so it's best to start learning about ajax techniques.http://www.w3schools.com/ajax/default.asp

Link to comment
Share on other sites

You'll want to use ajax for that, so it's best to get started learning ajax. When you're comfortable with how ajax works and the ideas behind it, then you can get started with that. Printing those things on the page shouldn't be difficult for you, you were looping through records in the other thread so you should at least know how to print all of the fields and icons out. The icons will need to use Javascript to do the calculations and send everything to the database though, so it's best to start learning about ajax techniques.http://www.w3schools.com/ajax/default.asp
OKI have been reading both ajax and javascript.And coming up with this...I know...I'm not finished.
<html><head><script type="text/javascript">function sendDataBtoC()	{	document.myForm1.C.value=document.myForm1.B.value;	}function sendDataCtoB()	{	document.myForm1.B.value=document.myForm1.C.value;	}  </script></head><body><form name="myForm1">Value A: <input type="text" name="A" size=4 >Value B: <input type="text" name="B" size=4 > Value C: <input type="text" name="C" size=4 ><input type="button" onclick="sendDataBtoC()" value="Send data B to C"><input type="button" onclick="sendDataCtoB()" value="Send data C to B"><br /></form></body></html>

What to do know? Save this as a .js file and call it in the loop?Must I rename my variables like A($i) where $i is the loop variable?

Link to comment
Share on other sites

If these are going in a table, then you need to give each element a unique ID (don't use names to refer to things in Javascript), and you need to pass the ID that you want to change to the function. So, here's a loop to write out a bunch of rows:

<?phpecho '<table>';for ($i = 0; $i < 10; $i++){  echo '<tr>';    echo '<td>Value A: <input type="text" size=4 id="A' . $i . '"></td>';  echo '<td>Value B: <input type="text" size=4 id="B' . $i . '"></td>';  echo '<td>Value C: <input type="text" size=4 id="C' . $i . '"></td>';  echo '</tr>';}echo '</table>';?>

Run that in a browser, then look at the source in the browser and see what it did. You should have fields with IDs like "A0", "A1", "A2", etc. Each field has a unique ID.Now you could add the button to the row, and have the button send the correct ID to the function:

<?phpecho '<table>';for ($i = 0; $i < 10; $i++){  echo '<tr>';    echo '<td>Value A: <input type="text" size=4 id="A' . $i . '"></td>';  echo '<td>Value B: <input type="text" size=4 id="B' . $i . '"></td>';  echo '<td>Value C: <input type="text" size=4 id="C' . $i . '"></td>';    echo '<td><input type="button" onclick="sendDataBtoC(' . $i . ')" value="Send data B to C"></td>';  echo '</tr>';}echo '</table>';?>

So, when you run that in the browser and look at it, you'll see the buttons run things like this:sendDataBtoC(0)sendDataBtoC(1)sendDataBtoC(2)etcThen just set up your function to get that value and add it to the ID to look for:

function sendDataBtoC(id){  var B = document.getElementById("B" + id);  var C = document.getElementById("C" + id);  C.value = B.value;}

So now that one function can work on all of the elements on the page, assuming you pass the correct ID to it.

Link to comment
Share on other sites

You are God. Thanks for the tip, it now works perfect.When I finally saw that I mixed values with both "," and "." .Now I need to add a function to check the user type figures.And a script which changes "," to "." For example, "3,14" to "3.14"Any tip?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...