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
https://forums.phpfreaks.com/topic/82104-dynamic-variable/
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
https://forums.phpfreaks.com/topic/82104-dynamic-variable/#findComment-417284
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
https://forums.phpfreaks.com/topic/82104-dynamic-variable/#findComment-417320
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
https://forums.phpfreaks.com/topic/82104-dynamic-variable/#findComment-417409
Share on other sites

Archived

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

×
×
  • 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.