Click to See Complete Forum and Search --> : Help between PHP and HTML forms again


htmlperlman
03-30-2005, 12:20 PM
I have the following code:

<FORM ACTION="nextform.php" METHOD=POST>
<?php
// open the connection
$conn = mysql_connect("localhost","user","pass");
// pick the database to use
mysql_select_db("db1",$conn);
// create the SQL statement
$sql = "SELECT * FROM table";
// execute the SQL statement
$result = mysql_query($sql, $conn) or die(mysql_error());
//go through each row in the result set and display data
while ($newArray = mysql_fetch_array($result)) {
// give a name to the fields
$exe1 = $newArray['exe'];
echo "$exe1 &nbsp;&nbsp;&nbsp;";
$size1 = $newArray['size'];
echo "$size1 &nbsp;&nbsp;&nbsp;";
$pub1 = $newArray['pub'];
if ($pub1 != NULL) echo ("$pub1 &nbsp;&nbsp;&nbsp;");
else echo ('<input type=text name="newpub" size=50>');
$app1 = $newArray['app'];
if ($app1 != NULL) echo ("$app1 &nbsp;&nbsp;&nbsp;");
else echo ('<input type=text name="newapp" size=50>');
$vers1 = $newArray['vers'];
if ($vers1 != NULL) echo ("$vers1 &nbsp;&nbsp;&nbsp;");
else echo ('<input type=text name="newvers" size=20>');
$num1 = $newArray['number'];
echo "$num1 <br>";
}
?>
<br><center><input type=submit name="submit" value="SUBMIT NEW VALUES"></center>
</FORM>

SO THE PROBLEM IS THIS::::

Effectively for every entry in the table the script will always display the first 2 fields (exe1 and size1). Then for the next 3 fields it will either display the value, or if its null display a text box asking for entry of the value. The problem I have occurs when more than one entry has these fields missing.....as the script can only send the new values (called newpub, newapp and newvers) once and so will only send the last entry of values to the next page. What I want is for all text entered to be sent to the next page where it will be saved to the file.

Any ideas????

Cheers

Dave

MarkL
03-30-2005, 12:46 PM
You need to use an array to pass multiple values from a form. Something like this should work:

<FORM ACTION="nextform.php" METHOD=POST>
<?php
// open the connection
$conn = mysql_connect("localhost","user","pass");
// pick the database to use
mysql_select_db("db1",$conn);
// create the SQL statement
$sql = "SELECT * FROM table";
// execute the SQL statement
$result = mysql_query($sql, $conn) or die(mysql_error());
//go through each row in the result set and display data
while ($newArray = mysql_fetch_array($result)) {
// give a name to the fields
$exe1 = $newArray['exe'];
echo "$exe1 &nbsp;&nbsp;&nbsp;";
$size1 = $newArray['size'];
echo "$size1 &nbsp;&nbsp;&nbsp;";
$pub1 = $newArray['pub'];
if ($pub1 != NULL) echo ("$pub1 &nbsp;&nbsp;&nbsp;");
else echo ('<input type=text name="new[pub]" size=50>');
$app1 = $newArray['app'];
if ($app1 != NULL) echo ("$app1 &nbsp;&nbsp;&nbsp;");
else echo ('<input type=text name="new[app]" size=50>');
$vers1 = $newArray['vers'];
if ($vers1 != NULL) echo ("$vers1 &nbsp;&nbsp;&nbsp;");
else echo ('<input type=text name="new[vers]" size=20>');
$num1 = $newArray['number'];
echo "$num1 <br>";
}
?>
<br><center><input type=submit name="submit" value="SUBMIT NEW VALUES"></center> </FORM>

NogDog
03-30-2005, 12:46 PM
Cleanest solution might be to display values in disabled input boxes so that they get resent with the form:

$valueDisabled = "";
if ($pub1 != NULL)
{
$valueDisabled = " value='$pub1' disabled";
}
echo "<input type=text name='newpub' size=50$valueDisabled>";

(You might want to experiment with setting the input element to "readonly" instead of "disabled" to see which appearance/behavior you prefer.)

ShrineDesigns
03-30-2005, 12:47 PM
you need to use ex. $_POST['field_name'] or $_GET['field_name'] (depends on the form's method) instead of $field_name

96turnerri
03-30-2005, 12:54 PM
i think he meant about having input with same name and only takes the last one not all so...

create a table id on auto_increment in your database and construct the fields as so

else echo ('<input type=text name="newvers-'.$row["id"].'" size=20>');

then nextform.php would need to be edited to account for this

i am presuming it looks something like

$sql = "UPDATE table SET newvers = '".$_POST["newvers"]."'.........";
mysql_query($sql) or die(mysql_error());

it would need to now be
foreach($_POST as $key => $val) {
$array = explode("-", $val);
$$array[1] .= "-".$array[0];
}
while(count($$num) != 0) {
$parts = explode("-", $$num);
$sql = "UPDATE table SET newpub = '".$parts['1']."', newapp = '".$parts['2']."', newvers = '".$parts['3']."' WHERE id = '".$$num."'";
mysql_query($sql) or die(mysql_error());
$num++;
}

which should work, but is untested

htmlperlman
03-30-2005, 03:54 PM
hi again

could anyone perhaps try explain the last post for me....i think he got the idea of what i was wanting to do but it seemed a bit advanced and i got a bit lost. Sorry

I tried the first option and it didnt seem to work so it might be harder than first thought.

MarkL
03-31-2005, 10:04 AM
hi again

could anyone perhaps try explain the last post for me....i think he got the idea of what i was wanting to do but it seemed a bit advanced and i got a bit lost. Sorry

I tried the first option and it didnt seem to work so it might be harder than first thought.

Did you change the code in nextform.php to work with the array instead of the individual variables that you were using before?

htmlperlman
03-31-2005, 02:28 PM
I think i did. Well i tried....if you could tell me how i should change it id probably have a better idea?

cheers mate

MarkL
03-31-2005, 03:02 PM
If you could post the code snippet from newform.php that is working with these variables once they are passed on, I might be able to help with that.

htmlperlman
04-01-2005, 08:02 AM
ok thanks, at the moment I have just set the script up to display the values. I will be adding them to a file in the end but at first I just wanted to ensure that they were passing through correctly


<?php
$newp = $_POST[newpub];
$newa = $_POST[newapp];
$newv = $_POST[newvers];
echo "$newp $newa $newv ";
?>