Jump to content

how to pass variables as an array??


mcerveni

Recommended Posts

I have a form that has to submit multiple records in one submission.

My only problem is since i'm using ajax for the request..i'm not sure how to use the php arrays ...

 

 

My form:

formev0.jpg

php code to select the form and loop the records, and create the array:

function shiftBid() { 

$getShift = "SELECT * FROM shiftbid"; 
$result = mysql_query($getShift);

//Count table rows 
$count=mysql_num_rows($result);
?>

<table id="shiftTable"> 
<tr> <th> NO </th> <th> DAY </th> <th> SHIFT </th> <th> RANK </th> </tr>

<?php
while($row= mysql_fetch_array($result)) { 

?>

<tr> 
<td> <?php $id[]=$row['id']; ?> <?php echo $row['no'];?> <input type="hidden" id="no[]" name="no" value="<?php echo $row['no'];?>"> </td> 
<td> <?php echo $row['day'];?>  <input type="hidden" id="day[]" name="day" value="<?php echo $row['day'];?>"> </td> 
<td> <?php echo $row['shift'];?>  <input type="hidden" id="shift[]" name="shift" value="<?php echo $row['shift'];?>"></td> 
<td> <?php echo '<input type="text" name="rank" size="2" maxlength="2" id="rank[]"> '; ?>  </td> </tr>
<?php
} //while
?>

<tr><td><input type="submit" name="submit" value="Done" onclick="addShift();"> </td></tr> 

</table>
<?php }
?>

 

 

ajax code to submit the values:

function addShift() {
dimScreen();
document.getElementById('loaderBox').style.display="block";
document.getElementById('loaderBox').innerHTML = " <table> <tr><td> <img src='scripts/loader.gif'> </td><td> <b> Processing </b> </td></tr></table> "


var no = encodeURI(document.getElementById('no').value);
var day = encodeURI(document.getElementById('day').value);
var shift = encodeURI(document.getElementById('shift').value);
var rank = encodeURI(document.getElementById('rank').value);


var url = "include/process.php";  
var params = "no=" + no + "&day=" + day + "&shift=" + shift + "&rank=" + rank;
http.open("POST", url, true);



//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
var response = http.responseText;
document.getElementById('content').innerHTML = ''+response+ '';

}

}
http.send(params);

} //function

 

 

php code to query the results

$no = $_POST['no'];
$day = $_POST['day'];
$shift = $_POST['shift'];
$rank = $_POST['rank'];


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

$insertShift_sql = "INSERT INTO user_req (id, no, day, shift, rank) VALUES(0, '$no[$i]', '$day[$i]', '$shift[$i]', '$rank[$i]')";

}


   if( @mysql_query ($insertShift_sql) ){ 
           echo 'You\'re shift has been processed. Thank you. ';
           
           //echo $no[$i] .' ' . $day[$i] . ' ' . $shift[$i] . ' ' . $rank[$i] . '\n';
       } 
       else {
       echo 'An error has been encountered. Please refresh the page. ';
       }
    

 

 

Link to comment
https://forums.phpfreaks.com/topic/118940-how-to-pass-variables-as-an-array/
Share on other sites

  • 2 weeks later...

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.