Help with passing arrays between php scripts
Moderator: General Moderators
Help with passing arrays between php scripts
Can anybody put me on the right track on how to pass arrays between php scripts.
I thought it would be something like:
$preselectvar = array();
echo '<form action="test.php" method="post">';
echo ' <table border=0>';
echo ' <input type="hidden" name="preselectvar[]" value="'.$preselectvar.'">';
for ($i=0; $i< $num_results; $i++)
{
echo ' <tr>';
$row = mysql_fetch_array($result);
$name = htmlspecialchars(stripslashes($row["Variable"]));
$units= htmlspecialchars(stripslashes($row["Units"]));
$preselectvar[]=$name;
echo ' <td>'.$name.'</td>';
echo ' <td> <select name="constraint[]">';
echo ' <option value="*" selected>*';
echo ' <option value="=">=';
echo ' <option value=">">>';
echo ' <option value="<"><';
echo ' <option value=">=">>=';
echo ' <option value="<="><=';
echo ' </select></td>';
echo ' <td><input type=text maxlength=10 size=10 name="value['.$i.']"</td>';
echo ' <td>'.$units.'</td>';
echo ' </tr>';
}
echo ' </table>';
echo ' <table border=0>';
echo ' <tr>';
echo ' <td colspan=2><input type=submit value="Submit">';
echo ' <input type=reset value="Reset"></td>';
echo ' </tr>';
echo ' </table>';
echo '</form>';
?>
and that I can use the two arrays in test.php with:
$constraint=$_POST("constraint");
$preselectvar=$_POST("preselectvar");
But this doesn't work in either case.
So where do I look, or what is the solution ???
thanx in advance
I thought it would be something like:
$preselectvar = array();
echo '<form action="test.php" method="post">';
echo ' <table border=0>';
echo ' <input type="hidden" name="preselectvar[]" value="'.$preselectvar.'">';
for ($i=0; $i< $num_results; $i++)
{
echo ' <tr>';
$row = mysql_fetch_array($result);
$name = htmlspecialchars(stripslashes($row["Variable"]));
$units= htmlspecialchars(stripslashes($row["Units"]));
$preselectvar[]=$name;
echo ' <td>'.$name.'</td>';
echo ' <td> <select name="constraint[]">';
echo ' <option value="*" selected>*';
echo ' <option value="=">=';
echo ' <option value=">">>';
echo ' <option value="<"><';
echo ' <option value=">=">>=';
echo ' <option value="<="><=';
echo ' </select></td>';
echo ' <td><input type=text maxlength=10 size=10 name="value['.$i.']"</td>';
echo ' <td>'.$units.'</td>';
echo ' </tr>';
}
echo ' </table>';
echo ' <table border=0>';
echo ' <tr>';
echo ' <td colspan=2><input type=submit value="Submit">';
echo ' <input type=reset value="Reset"></td>';
echo ' </tr>';
echo ' </table>';
echo '</form>';
?>
and that I can use the two arrays in test.php with:
$constraint=$_POST("constraint");
$preselectvar=$_POST("preselectvar");
But this doesn't work in either case.
So where do I look, or what is the solution ???
thanx in advance
- Bill H
- DevNet Resident
- Posts: 1136
- Joined: Sat Jun 01, 2002 10:16 am
- Location: San Diego CA
- Contact:
You need to specify "multiple" in the select line
Code: Select all
print("<SELECT MULTIPLE NAME=Namesї] SIZE=15>\n");
while ($row = mysql_fetch_array($Result))
print("<OPTION value=$rowїid]>$rowїName]</OPTION>\n");
print("</SELECT>\n");
Last edited by Bill H on Fri Sep 20, 2002 8:58 am, edited 2 times in total.
- Bill H
- DevNet Resident
- Posts: 1136
- Joined: Sat Jun 01, 2002 10:16 am
- Location: San Diego CA
- Contact:
Sorry, your post was a bit unclear. I just realized all the selection box code you posted has nothing to do with your question. Or maybe it dies, since you are showing constraint to be an array. If it is an array then you need the "multiple" as shown. If it is not, what is the question about arrays?
After several readings, I'm not real clear on what your question is. Maybe somebody else can help you.
After several readings, I'm not real clear on what your question is. Maybe somebody else can help you.
In your statement of problem:
the parentheses '(' and ')' should be square brackets '[' ']'. Was this a typo in the post or is it also in the code?$constraint=$_POST("constraint");
$preselectvar=$_POST("preselectvar");
Last edited by nielsene on Fri Sep 20, 2002 9:19 am, edited 1 time in total.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
To enable us to help you better could you please expand on:
Also disregard this if it was just a typo when you posted but if you copied and pasted this code from the file you're trying to run - have you had a look at your source code?
This:
will become
you need to close the input tag:
Mac
Are there any error messages, what's happening/not happening that shouldn't/should happen?But this doesn't work in either case.
Also disregard this if it was just a typo when you posted but if you copied and pasted this code from the file you're trying to run - have you had a look at your source code?
This:
Code: Select all
echo ' <td><input type=text maxlength=10 size=10 name="valueї'.$i.']"</td>';Code: Select all
<td><input type=text maxlength=10 size=10 name="valueї1]"</td>Code: Select all
echo '<td><input type="text" maxlength="10" size="10" name="valueї'.$i.']" /></td>';Sorry, I just realised I mad a lot of typos, and not alone in this post :O(
The () in stead of [] in _POST was a typo in the mail
Everything is working fine (I just needed to realise indeed that the hidden field needs to be in a loop to create a hidden entry for each preselectvar like :
for ($i=0; $i< $num_results; $i++)
{
echo ' <input type="hidden" name="preselectvar['.$i.']"value='.$preselectvar[$i].'>';
}
it is working now, but the only thing that puzzles me is now:
say the preselectvar has 6 entries:
1
2
3
4
5
6
then in the test.php script I get :
$preselectvar[]=$HTTP_POST_VARS["preselectvar"]
for ($i=0; $i < sizeof($preselectvar);$i++)
{
echo 'preselectvar['.$i.']: '.$preselectvar[$i].'<br>';
}
this does give me:
1
2
3
4
5
6
Array
This array also is an extra entry in the other arrays I pass.
is that always the case with arrays ??? thus should I discard the last entry in all cases. Or should I reconstruct the array in another way ?
Thanx
The () in stead of [] in _POST was a typo in the mail
Everything is working fine (I just needed to realise indeed that the hidden field needs to be in a loop to create a hidden entry for each preselectvar like :
for ($i=0; $i< $num_results; $i++)
{
echo ' <input type="hidden" name="preselectvar['.$i.']"value='.$preselectvar[$i].'>';
}
it is working now, but the only thing that puzzles me is now:
say the preselectvar has 6 entries:
1
2
3
4
5
6
then in the test.php script I get :
$preselectvar[]=$HTTP_POST_VARS["preselectvar"]
for ($i=0; $i < sizeof($preselectvar);$i++)
{
echo 'preselectvar['.$i.']: '.$preselectvar[$i].'<br>';
}
this does give me:
1
2
3
4
5
6
Array
This array also is an extra entry in the other arrays I pass.
is that always the case with arrays ??? thus should I discard the last entry in all cases. Or should I reconstruct the array in another way ?
Thanx
are you still doing
after your loop to populate it? If so that is what is adding the array entry at the end.
Code: Select all
echo ' <input type="hidden" name="preselectvarї]" value="'.$preselectvar.'">';No,
I do as I showed in the post above yours:
for ($i=0; $i < sizeof($preselectvar);$i++)
{
echo 'preselectvar['.$i.']: '.$preselectvar[$i].'<br>';
}
But it does the same for the other two arrays. One in the loop as select and one in the loop as input type=text. Those two also have array entered as last entry
I do as I showed in the post above yours:
for ($i=0; $i < sizeof($preselectvar);$i++)
{
echo 'preselectvar['.$i.']: '.$preselectvar[$i].'<br>';
}
But it does the same for the other two arrays. One in the loop as select and one in the loop as input type=text. Those two also have array entered as last entry
I did see however that if I do a :
print_r($HTTP_POST_VARS["preselectvar"]);
it does give me the expected output :
Array ( [0] => Config [1] => nb_in1 [2] => nb_in2 [3] => nb_out1 [4] => squewing [5] => unfolding )
So isn't the
preselectvar[]=HTTP_POST_VARS["preselectvar"];
the one that is wrong ???
print_r($HTTP_POST_VARS["preselectvar"]);
it does give me the expected output :
Array ( [0] => Config [1] => nb_in1 [2] => nb_in2 [3] => nb_out1 [4] => squewing [5] => unfolding )
So isn't the
preselectvar[]=HTTP_POST_VARS["preselectvar"];
the one that is wrong ???
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Massive Technique Instance Pre-Selector </title>
</head>
<body>
<h1> Massive Technique Instance Pre-Selector </h1>
<?php
if (! $db)
{
echo "Error: Could not connect to database. Please try again later.";
exit;
}
mysql_select_db("exploration");
$technique_name=$HTTP_POST_VARS["technique_name"];
echo '<form action="tinsert_technique_values.php" method="post">';
echo ' <table border=0>';
echo ' <input type="hidden" name="technique_name" value="'.$technique_name.'">';
echo ' <tr>';
echo ' <td><strong>Give search constraints for:</strong></td>';
echo ' <td><strong>'.$technique_name.'</strong> </td>';
echo ' </tr>';
echo ' <tr><td><strong> "*" = any </strong></td></tr>';
echo ' <tr><td><strong> "=" = exact match </strong></td></tr>';
echo ' <tr><td><strong> ">" = all nr bigger then nr </strong></td></tr>';
echo ' <tr><td><strong> "<" = all nr smaller then nr </strong></td></tr>';
echo ' <tr><td><strong> ">=" = all nr bigger or equal then nr </strong></td></tr>';
echo ' <tr><td><strong> "<=" = all nr smaller or equal then nr </strong></td></tr>';
echo ' </table>';
$query = "select Variable,Units from technique_variables where Technique='".$technique_name."' and KindID=2 and (TypeID=2 || TypeID=3) ";
$result = mysql_query($query);
if (!$result)
{
echo " Error: something went wrong with query: ".$query;
exit;
}
$num_results = mysql_num_rows($result);
echo ' <table border=3 cols='.$num_results.' bgcolor="#CCCCCC" >';
for ($i=0; $i< $num_results; $i++)
{
echo ' <tr>';
$row = mysql_fetch_array($result);
$name = htmlspecialchars(stripslashes($row["Variable"]));
$units= htmlspecialchars(stripslashes($row["Units"]));
$preselectvar[]=$name;
echo ' <td>'.$name.'</td>';
echo ' <td> <select name="conditions['.$i.']">';
echo ' <option value="*" selected>*';
echo ' <option value="=">=';
echo ' <option value=">">>';
echo ' <option value="<"><';
echo ' <option value=">=">>=';
echo ' <option value="<="><=';
echo ' </select></td>';
echo ' <td><input type=text maxlength=10 size=10 name="values['.$i.']"</td>';
echo ' <td>'.$units.'</td>';
echo ' </tr>';
}
for ($j=0; $j< $num_results; $j++)
{
echo ' <input type="hidden" name="preselectvar['.$j.']"value='.$preselectvar[$j].'>';
}
echo ' </table>';
echo ' <table border=0>';
echo ' <tr>';
echo ' <td colspan=2><input type=submit value="Submit">';
echo ' <input type=reset value="Reset"></td>';
echo ' </tr>';
echo ' </table>';
echo '</form>';
?>
</body>
</html>
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Massive Technique Instance Pre-Selector </title>
</head>
<body>
<h1> Massive Technique Instance Pre-Selector </h1>
<?php
if (! $db)
{
echo "Error: Could not connect to database. Please try again later.";
exit;
}
mysql_select_db("exploration");
$technique_name=$HTTP_POST_VARS["technique_name"];
echo '<form action="tinsert_technique_values.php" method="post">';
echo ' <table border=0>';
echo ' <input type="hidden" name="technique_name" value="'.$technique_name.'">';
echo ' <tr>';
echo ' <td><strong>Give search constraints for:</strong></td>';
echo ' <td><strong>'.$technique_name.'</strong> </td>';
echo ' </tr>';
echo ' <tr><td><strong> "*" = any </strong></td></tr>';
echo ' <tr><td><strong> "=" = exact match </strong></td></tr>';
echo ' <tr><td><strong> ">" = all nr bigger then nr </strong></td></tr>';
echo ' <tr><td><strong> "<" = all nr smaller then nr </strong></td></tr>';
echo ' <tr><td><strong> ">=" = all nr bigger or equal then nr </strong></td></tr>';
echo ' <tr><td><strong> "<=" = all nr smaller or equal then nr </strong></td></tr>';
echo ' </table>';
$query = "select Variable,Units from technique_variables where Technique='".$technique_name."' and KindID=2 and (TypeID=2 || TypeID=3) ";
$result = mysql_query($query);
if (!$result)
{
echo " Error: something went wrong with query: ".$query;
exit;
}
$num_results = mysql_num_rows($result);
echo ' <table border=3 cols='.$num_results.' bgcolor="#CCCCCC" >';
for ($i=0; $i< $num_results; $i++)
{
echo ' <tr>';
$row = mysql_fetch_array($result);
$name = htmlspecialchars(stripslashes($row["Variable"]));
$units= htmlspecialchars(stripslashes($row["Units"]));
$preselectvar[]=$name;
echo ' <td>'.$name.'</td>';
echo ' <td> <select name="conditions['.$i.']">';
echo ' <option value="*" selected>*';
echo ' <option value="=">=';
echo ' <option value=">">>';
echo ' <option value="<"><';
echo ' <option value=">=">>=';
echo ' <option value="<="><=';
echo ' </select></td>';
echo ' <td><input type=text maxlength=10 size=10 name="values['.$i.']"</td>';
echo ' <td>'.$units.'</td>';
echo ' </tr>';
}
for ($j=0; $j< $num_results; $j++)
{
echo ' <input type="hidden" name="preselectvar['.$j.']"value='.$preselectvar[$j].'>';
}
echo ' </table>';
echo ' <table border=0>';
echo ' <tr>';
echo ' <td colspan=2><input type=submit value="Submit">';
echo ' <input type=reset value="Reset"></td>';
echo ' </tr>';
echo ' </table>';
echo '</form>';
?>
</body>
</html>
Last edited by coolen on Sat Sep 21, 2002 8:36 am, edited 1 time in total.