Jump to content

dynamic variable


optikalefx

Recommended Posts

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.

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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;

?>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.