Jump to content

[SOLVED] Updating array before passing it in Form


DazlerD

Recommended Posts

Hi

 

I have a simple section to my website that allows the user to type in a product code and see all the accessories associated with that product in a table.  The accessories are retrieved from a MySQL database using the product code.

 

There are only 3 columns, partcode, description and qty.  Partcode and description  are readonly and qty is entered by the user in a text field.

 

When the user presses 'Next' I am passing the array of partcodes, qtys back to the same php page so the values can be validated.  (i.e. There is at least one quantity > 0).  I have the following:

 

TO MAKE THE ARRAY

$c=0;

$a_acc = array();

// Make the query.

$query = "SELECT description, partcode, 0 FROM accessories WHERE productcode = '$productcode';";

$result = mysql_query ($query); // Run the query.

if (mysql_num_rows($result) == 0) {

$errors[] = 'We could not get the accesories due to a system error. We apologize for any inconvenience.'; // Public message.

$errors[] = mysql_error() . '<br /><br />Query: ' . $query; // Debugging message.

} else {

while($row = mysql_fetch_array($result))

{

$a_acc[$c][0] = $row['description']; // description

$a_acc[$c][1] = $row['partcode']; // partcode

$a_acc[$c][2] = $row[2]; // quantity

$c++;

}

$c--;

}

 

 

THE FORM

 

<form action="accessory2.php" name="accessory2" method="post">

 

<table width="400" cellspacing="0" cellpadding="5" border="1">

<tr>

<td class="tabletop" align="center" colspan="3"><b>Spares Available</b></td>

</tr>

<tr>

<td align="left"><b>Description</b></td>

<td align="left"><b>Product Code</b></td>

<td align="left"><b>Qty Required</b></td>

</tr>

 

<?php

$d = 0;

$bg = '#eeeeee';

while($d <= sizeof($a_acc) - 1 ){

$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');

echo '<tr bgcolor="'.$bg.'">

<td align="left">' . $a_acc[$d][0] . '</td>

<td align="left">' . $a_acc[$d][1] . '</td>

<td align="left"><input type="text" maxLength="5" name="item'.$d.'" size="10" value="0"/></td>

</tr>';

$d++;

}

?>

</table>

<p><p><p>

 

<table width="400">

<tr>

  <td width="200" align="left"><input type="button" name="back" value="Back" onclick='goBack()'/></td>

  <td width="200" align="right"><input type="submit" name="submit" value="Next" onclick='validateQty()';></td>

</tr>

</table>

 

<input type="hidden" name="a_acc" value="<?php echo base64_encode(serialize($a_acc)); ?>"/>

<input type="hidden" name="submitted" value="TRUE" />

 

</form>

 

<SCRIPT LANGUAGE='JAVASCRIPT'>

function validateQty()

{

<?php $a_acc[0][2] = '10' ?>;

alert("Hello out there");

}

</SCRIPT>

 

The array is passed back to the form Ok except the quantity is never being added.  Is this because the current state of the a_acc array is set in the form before the function validateQty is run?

 

Thanks for any help

 

Darren

 

I assume you are using PHP to write Javascript?

 

Have you pasted all the code as you see it?

 

Because whatever is in the JS function looks wrong:

 

<SCRIPT LANGUAGE='JAVASCRIPT'>
function validateQty()
{
<?php $a_acc[0][2] = '10' ?>;
alert("Hello out there");
}
</SCRIPT>

 

You have PHP in the javascript. Are you trying to use PHP to dynamically write JS? Or is this just wrong?

 

monk.e.boy

Hi Monk.e.boy

 

I tried to update the quantity part of the array by writting dynamic PHP in a 'for' loop.

 

I couldnt get this to work so wrote it as is just to test.  Im not sure of the syntax I must admit but have tried loads of ways but the quantity never gets updated when the page is submitted.

 

I do see the alert so it cant be causing an error.

 

I thought I had to use the PHP syntax around the a_acc variable as its a variable declared in php?

 

Thanks

 

Darren

 

 

erm I see know that Im trying to interchange code that runs on the server (php) with client side code (Javascript).  Bugger.

 

So, how can I update my array a_acc with the quanities the users has entered in the input boxes so I can pass it as a POST variable?

Right sorted it.

 

All i do now is pass the values from the text fields into the array after the form has been submitted.

 

for ($i = 0; $i < count($array); $i++)

{

  $name = 'item'.$i;

  $a_acc[$i][2] = $_POST["$name"];

}

 

Then check for quantities.

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.