/    Sign up×
Community /Pin to ProfileBookmark

sending muliple forms

hi,

Im trying to something quite tricky, but im stuck on something not so hard ( i think)

for an array of variable, eg fruits=(bannana, apple, pear)

how can I get a form to submit each value of FRUIT without going back and resending the forum for each value.

eg [0]bannana … submit
[1]apple … submit
[2]pear … submit.

I do not want to send multiple varibales (its actually quite a complicated php file that im sendinmg to, and i may need to send thouasands of same name request one after another)

I do wnat to submit multiple times, miving down the array each time (without having to do this manually)

to post a comment
HTML

9 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayMay 24.2007 — This should be close:
<i>
</i>var fruit = ["bannana", "apple", "pear",....];
for (i=0;i&lt;fruit.length;i++){
document.form1.fruitSel.value=fruit[i];
document.form1.submit();
}
...
&lt;form name="form1"....
&lt;input name="fruitSel" type="text" /&gt;
....
Copy linkTweet thisAlerts:
@divxauthorMay 24.2007 — yes i though about js, but then though that this method would send the first value, then the page would be redirected to the method (e.g. method=myPhp.php)

then the 2nd post value would never be sent..

or that the js could send each all at once, but I cant do this all at once (it has to be one after another as its being sent to the same php file (which does quite a bit)
Copy linkTweet thisAlerts:
@TheBearMayMay 24.2007 — Could end up with several open windows but use two different pages:

[code=html]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Driver</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
var fruit = ["bannana", "apple"];
for (i=0;i<fruit.length;i++){
newWin=window.open("wd5.htm?fruit="+fruit[i],"newWin"+i);
}

</script>
</head>

<body>

</body>
</html>
[/code]
and
[code=html]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Submitter</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<script type="text/javascript">
window.onload=function(){
var fruitPassed=document.location.search.substr(7);
document.getElementById("fruitSel").value=fruitPassed;
document.form1.submit();
}
</script>
<body>

<form name="form1" action="newform.htm">
<input name="fruitSel" id="fruitSel" type="text" />
</form>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@TheBearMayMay 24.2007 — or if you know what the query string should look like you could construct it directly and bypass the form altogether:

[code=html]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Driver</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
var fruit = ["bannana", "apple"];
for (i=0;i<fruit.length;i++){
newWin=window.open("myPhp.php?fruit="+fruit[i],"newWin"+i);
}
</script>
</head>

<body>

</body>
</html>[/code]
Copy linkTweet thisAlerts:
@divxauthorMay 24.2007 — eeek, yer I see where your going, but this will only work if method is get, It wouldnt be any effort to change the php post statements.

However, this isnt going to work with >1000 fruits, a thousand pages open all at once will kill the host.

Im wondering if the php will run if i submit to a named window and submit each "fruit" to the same window (e.g take away i above)..hmmm

if the php is reached (and then somthing is loaded into this page, will the php still carry on excecuting?) - i never used to be a php programmer, but have dived deep into it lately out of necessity

I tried out the first method, and it did as i expected (only sent one variable successfully)
Copy linkTweet thisAlerts:
@divxauthorMay 24.2007 — just in case anyone intreasted im posting the 1st method php version here:

(its not fruits, but urls)
[code=php]<?php

mysql_connect("ipaddress", "username", "pass") or die(mysql_error());
mysql_select_db("Thedb") or die(mysql_error());
$data = mysql_query("SELECT URL FROM Users where 1")or die(mysql_error());

print ' <html>
<body>
<font>update all the HTML Files from the database</font>

<form name="upDateName" action="MyPhp.php" method="post" style="margin: 0;">
<input type="text" name="URL" >
<input type="submit" value="upDate"></form></body></html>';

print '
<script type="text/javascript" language="JavaScript">
var URLArray =new Array();';
$id = 0;
While ($fetchedURLs = mysql_fetch_array( $data )){
$fullURL = $fetchedURLs['LT_URL'];
$localURL = substr($fullURL, 36, strlen($fullURL));
print 'URLArray['.$id.'] = "'.$localURL.'";';
$id++;
}
print '
for (i=0; i<URLArray.length; i++){
document.upDateName.URL.value="'.$localURL.'";
document.upDateName.submit();
document.write( URLArray[i] + "<br>");
}
</script> ';
?>[/code]
Copy linkTweet thisAlerts:
@divxauthorMay 24.2007 — I tried the follwing, but only excuted the last php script, so it seems php needs to be executing in the browser for it to complete.

[code=php]<?php

mysql_connect("theip", "username", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$data = mysql_query("SELECT URL FROM Users where 1")or die(mysql_error());

print ' <html>
<body>
<font>update all the HTML Files from the database</font>

<form name="upDateName" action="myPHP.php" method="get" style="margin: 0;">
<input type="text" name="ltURL" >
<input type="submit" value="upDate"></form></body></html>';

print '
<script type="text/javascript" language="JavaScript">
var URLArray =new Array();';
$id = 0;
While ($fetchedURLs = mysql_fetch_array( $data )){
$fullURL = $fetchedURLs['LT_URL'];
$localURL = substr($fullURL, 36, strlen($fullURL));
print 'awin = window.open("myPHP.php?ltURL="+"'.$localURL.'","newWin");';
$id++;
}
print '</script>';
?>[/code]


so now im thinking, maybe i could just pause for 30seconds before submitting the next window
Copy linkTweet thisAlerts:
@TheBearMayMay 25.2007 — This should open a new one every 30 seconds....
[code=html]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Driver</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
var fruit = ["bannana", "apple"];
var i=0;
function sendFruit(){
newWin=window.open("wd5.htm?fruit="+fruit[i],"newWin"+i);
i++;
alert(i +":"+fruit.length);
if (i < fruit.length) setTimeout("sendFruit()",30000);
}
sendFruit();
</script>
</head>

<body>

</body>
</html>[/code]
Copy linkTweet thisAlerts:
@NogDogMay 27.2007 — Not sure I understand what you want to do, but if you name multiple form fields with the same name, and that name ends with "[]", then those form field values will be submitted as an array of values on the PHP side, e.g.:
[code=html]
<label>Apple: <input type="checkbox" name="fruit[]" value="apple"></label>
<label>Banana: <input type="checkbox" name="fruit[]" value="banana"></label>
<label>Orange: <input type="checkbox" name="fruit[]" value="orange"></label>
[/code]

Then in your PHP script, the values from those fields which were checked would be in an array called $_POST['fruit'] ($_POST['fruit'][0], $_POST['fruit'][1], etc.). And of course you could do the same thing with hidden fields or text fields, as appropriate to whatever you're actually trying to do.
×

Success!

Help @divx spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...