Forum Moderators: coopster

Message Too Old, No Replies

Need Help With Script

         

spyder_tek

6:41 am on Jan 31, 2006 (gmt 0)

10+ Year Member



Hello,

I'm working on a script that submits user input from 3 different text boxes and submits this info to several different form actions.

Below is my script:


<?
if (isset($_POST["submit"]))
{
$dest = array ("http://www.site1.com/form1.asp","http://www.site2.com/form2.php","http://www.site3.com/form3.jsp");

//form1
$hiddenvalue[0]["somevalue"] = $_POST["input1"];

//form2
$hiddenvalue[1]["somevalue"] = "1";
$hiddenvalue[1]["somevalue"] = $_POST["input2"];

//form3
$hiddenvalue[2]["somevalue"] = "5";
$hiddenvalue[2]["somevalue"] = $_POST["input3"];

$submit_to = array();
if (isset ($_POST["destination"]))
{
for ($i=0; $i<count($_POST["destination"]); $i++)
{
$submit_to[] = $dest[$_POST["destination"][$i]];
}
}

foreach ($submit_to as $i => $url)
{
$connection = curl_init($url);
$postvar = ($hiddenvalue[$i])? array_merge ($_POST,$hiddenvalue[$i]) : $_POST;
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $postvar);
$result = curl_exec($connection);
echo "Form No ".($i+1).":<br>";
print_r($result);
curl_close($connection);
}
Header ("Location: thank_you.html");
}
?>

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<form name="send" action="index.php" method="post">

Data1:
<input type="text" name="input1">

Data2:
<input type="text" name="input2">

Data3:
<input type="text" name="input3">

<p>
Submit form to:
<input type="checkbox" name="destination[]" value="0">
<input type="checkbox" name="destination[]" value="1">
<input type="checkbox" name="destination[]" value="2">
</p>

<input type="submit" value="Submit" name="submit">

</form>
</body>
</html>

Is there a way to submit these forms without using an array? So that instead of it looping through each form action, it only submits according to which checkboxes are selected?

So if only checkbox value="2" is selected it would submit only to:

[site3.com...]

The reason I'm concerned about this, is the fact that everything seems too slow, and more importantly, data isn't passed to some forms. I suspect the array is messing something up.

All help is appreciated, thanks!

stee

9:13 am on Jan 31, 2006 (gmt 0)

10+ Year Member



spyder_tek,

you mentioned there may be an array problem - i believe you are over-writing a couple of your array values in the code below:


//form2
$hiddenvalue[1]["somevalue"] = "1";
$hiddenvalue[1]["somevalue"] = $_POST["input2"];

//form3
$hiddenvalue[2]["somevalue"] = "5";
$hiddenvalue[2]["somevalue"] = $_POST["input3"];

if you want to simply append another value to the $hiddenvalue[1]["somevalue"] array, then you need to add "[]" at the end:


$hiddenvalue[2]["somevalue"][] = "5";
$hiddenvalue[2]["somevalue"][] = $_POST["input3"];

or you may not need so many dimensions...


$hiddenvalue[2][] = "5";
$hiddenvalue[2][] = $_POST["input3"];

also:

Header ("Location: thank_you.html");

has to be used before any output to the browser, so before any 'echo' or 'print' statements.

if you want to submit to a single form, depending on which input data is received, the all you'd need is a series of 'if' statements:


// if input1, whatever the values of input2/3...
if( isset( $_POST["input1"] ) )
{
$dest = "http://www.site1.com/form1.asp";
}

// else if input2 AND input3...
else if( isset( $_POST["input2"] ) && isset( $_POST["input3"] ) )
{
$dest = "http://www.site2.com/form2.php":
}

// else if input3...
else if( isset( $_POST["input3"] )
{
$dest = "http://www.site3.com/form3.jsp";
}

// else if nothing submitted... for example...
else
{
$dest = "errorpage.php";
}

becareful of your ordering of the above... for example if the user were to post all three variables, only the first statement would be executed.

hope this helps,

stee

spyder_tek

2:29 am on Feb 3, 2006 (gmt 0)

10+ Year Member



Thank you for your help!