optikalefx Posted December 17, 2007 Share Posted December 17, 2007 ok same project one last tidbit. i have var 'i' changing throught the script. How can i send that as a hidden variable to the form when you submit? I tried <input type="hidden" and sending it thru the URL but the problem is that that is all written as soon as the script runs, so 'i' is at its beginnign value. How can i send it the changed 'i'. As in don't write the hidden variable until you submit the script. I tried making an onsubmit function that did document.write('<input type="hidden" blah but that didnt do it fast enough for the script to send, so i was never sent. If i could get a function that sends a variable along with the submit, then maybe it would work. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 18, 2007 Share Posted December 18, 2007 <script language="javascript"> var i="3"; function preSubmit() { document.getElementById('addIn').innerHTML='<input type="hidden" value="'+i+'" name="whatever">'; setTimeout("document.FormName.submit()", 1000); } </script> <form name="FormName"> <span id="addIn"></span> <input type="button" value="Submit" onclick="preSubmit()"> </form> Quote Link to comment Share on other sites More sharing options...
optikalefx Posted December 18, 2007 Author Share Posted December 18, 2007 sounded too good to be true. It still submits i=3 each time even if i is 5. i have a alert(i) function that tells me what i is, and it says its 5, even tho the php page pulls up 3, no matter what i is. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 18, 2007 Share Posted December 18, 2007 I just set the "i" variable to "3" - because of your previous code - you can change it with JS, PHP, or whatever you choose. Quote Link to comment Share on other sites More sharing options...
optikalefx Posted December 18, 2007 Author Share Posted December 18, 2007 thats the problem tho. i set it to 3. but then i have the rest of the code change it. heres the whole code so im not staying in the dark. <html> <head> <script language="javascript"> var i = 3; function preSubmit() { document.getElementById('addIn').innerHTML='<input type="hidden" value="'+i+'" name="ivar">'; setTimeout("document.xmlCreator.submit()", 1000); } function reti(i) { alert(i); } function addField(i) { i++; document.getElementById('fields').innerHTML += "<span id='db" + i + "'>Data field " + i + ": <input type='text' name='db" + i + "'><br>\n"; return i; } function delField(i) { document.getElementById('db'+i).innerHTML = ""; i--; return i; } function liveSearch() { if(document.xmlCreator.livesearch.checked == true) { document.getElementById('liveSearches').innerHTML += "Which field to Display?: <input type'text' size='2' name='ls'><br>\n"; } else { document.getElementById('liveSearches').innerHTML = ""; } } </script> </head> <div id="start"> <form name="xmlCreator" method="post" action="http://www.4tenonline.com/databaseCreator.php?ivar=3"> </div> Database name: <input type="text" name="database_name"><br> <div id="fields"> <span id="db1">Data field 1: <input type="text" name="db1"><br> <span id="db2">Data field 2: <input type="text" name="db2"><br> <span id="db3">Data field 3: <input type="text" name="db3"><br> </div> <input type="button" onClick="reti(i);" value="What is i"> <input type="button" onClick="i = addField(i);" value="Add a field"> <input type="button" onClick="i = delField(i);" value="Delete a field"><br> Live Search? <input type="checkbox" name="livesearch" onchange="liveSearch();"><br> <div id="liveSearches"> </div> <br> <span id="addIn"></span> <input type="submit" value="Make Database" onclick="preSubmit()"> </form> </html> i is changing when you "add fields" and such. and i know its changing cuz the alert(i) works. but i cant seem to send it to the php script. which is this if you are wondering. <?PHP $i = $_REQUEST["ivar"]; $dataName = $_REQUEST["database_name"]; $ls = $_REQUEST["ls"]; for ($j = 0 ; $j<=$i ; $j++) { $dab[$j] = $_REQUEST["db".$j]; echo $dab[$j] . "<br>"; } echo "<br>i is " . $i; echo "<br>j is " . $j; echo "<br>dataName is " . $dataName; echo "<br>ls is " . $ls; ?> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 18, 2007 Share Posted December 18, 2007 Hmmmm...I believe you are going about things a little bit awkwardly here. First off, while innerHTML does work, I recommend using DOM methods to dynamically add and remove contents from your form. While it takes a slight adjustment to get used to, it does simplify things in the long run IMO. Additionally, it looks like your Javascript only allows to add a row at the end of existing rows and also only allows to delete the last row added. What if the user wants to insert a row or delete a middle row? Secondly, I'm assuming you are sending the i value to indicate to your PHP script how many rows exist. This is totally unnecessary. Instead of naming your inputs db1, db2, db3, ..., dbi, just name them db[]. This will send their contents as an array and you can just do this in your PHP code: foreach($_POST['db'] as $db){ // process the field } The beauty of sending the db fields as an array is then you can easily allow the insert to insert or delete any row in your form without having to go back and rename the other ones. Quote Link to comment Share on other sites More sharing options...
optikalefx Posted December 18, 2007 Author Share Posted December 18, 2007 i may just be naiive but I dont see/know any dom methods to 'add' input fields? I must be missing something Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 18, 2007 Share Posted December 18, 2007 var input = document.createElement('input'); input.setAttribute('type', 'text'); input.setAttribute('size', '40'); // assuming insertPoint is the node to insert under insertPoint.appendChild(input); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.