methuselah
methuselah

Reputation: 13206

Posting multiple radio button values to MySQL using "foreach"

I have adjusted my code slightly but I am still having difficulty posting it to the table. Would someone please provide me with an example of the foreach array?

Form page

  <div style="padding: 15px;">

<span class="loginfail" style="font-size:24px; font-weight: bold">Notifications</span><p>

<?php include("progress_insertcomment.php"); ?>

 <?php 

// Make a MySQL Connection
mysql_select_db("speedycm_data") or die(mysql_error());

$query_comment = "select * from tbl_alert order by id desc limit 1";
$comment = mysql_query($query_comment, $speedycms) or die(mysql_error());
$row_comment = mysql_fetch_assoc($comment);
$totalRows_comment = mysql_num_rows($comment);

?>

<!--- add notification --->

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <span id="sprytextarea1">

<textarea id='comment' name="comment" style="height: 75px; width:330px;"><?php echo $row_comment['comment']; ?></textarea> 
</span>
<p>
<button type="submit">Add</button>
               <input type="hidden" name="notc" value="1"/>
               </form>

               <!--- notification history --->

               <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

               <table border="0" cellspacing="2" cellpadding="2">
                  <?php

if ( $row_comment == 0 ) {

        echo "<span style='font-size: 11px;'>No current alerts.</span>";

    } else {

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM tbl_alert ORDER BY id DESC") 
or die(mysql_error());  

while($rows=mysql_fetch_array($result)){ ?>
  <tr>
    <td>
     <?php
echo "<div class='bubble'><div class='pimped'>
        <blockquote>" . $rows['comment'] . "
        </blockquote></div>
        <cite><strong>" . $rows['user'] . "</strong> @ " . $rows['date'] . "</cite>
        <span style='font-size: 10px;'>
        <p>
    <a href='editalert.php?id=". $rows['id'] ."' class='form' >Edit</a>&nbsp;&#8226;&nbsp;<a href='deletealert.php?id=". $rows['id'] ."' class='form'>Delete</a>
    </span>
    </div>
    "; 
    ?> 
    </td>
    <td valign="top" align="center"><div style="padding-left: 30px;"><span style="font-size: 10px;">Completed?</span>
    <p class="field switch">

    <!--- determine status of notification --->

    <?php $status = $rows['status']; ?>

    <input type="radio" name="selstatus" value="no" <?php if($status == 'yes') {echo 'checked';} else {echo '';} ?>/>
    <input type="radio" name="selstatus" value="yes" <?php if($status == 'yes') {echo 'checked';} else {echo '';} ?>/>
    <input type="hidden" name="statusid" value="<?php echo $rows['id']; ?>"/>
    <label for="radio1" class="cb-enable <?php if($status == 'yes') {echo 'selected';} else {echo '';} ?>"><span>Yes</span></label>
    <label for="radio2" class="cb-disable <?php if($status == 'no') {echo 'selected';} else {echo '';} ?>"><span>No</span></label>
    </p>
    </div></td>
  </tr>
  <tr>
    <td></td>
      <?php
    }
    }
    ?>
    <td align="center"><div style="padding-left: 30px;">
<button type="submit">Update</button>
        <input type="hidden" name="notc2" value="1"/>
    </div></td>
  </tr>
</table>
</form>

</div>
</body>

processing

           <?php   

        // 6) update notifications

        if (array_key_exists('notc2',$_POST)) {

echo "<p style='font-size: 12px;'>Thank you. The notifications have been updated successfully.<p>";

    echo "<p><span style='font-size: 12px;'>
                            <a onClick=\"history.go(-1)\" class='form'>Return</a></p>
                            <p></span>
                ";

            exit;

                };  
                ?>

After doing some research I learnt that the only way to insert multiple radio values into the MySQL table is to use arrays. There was a similar question raised somewhere on this site: PHP multiple radio buttons that recommended using a foreach loop.

Use a foreach loop

<?php
foreach ( $_POST as $key => $val )
    echo "$key -> $val\n";
?>

$key will be the name of the selected option and $val, well, the value.

How would this apply to my situation? - I am struggling to find any help on the internet. I understand that I would use this on the processing page to pull out any radio values from the previous page and then loop the INSERT MySQL code within the process until it was all done.

Upvotes: 0

Views: 8127

Answers (1)

Wrikken
Wrikken

Reputation: 70490

As far as I gather, these are multiple rows. To get PHP to parse a POST in a way you'll find a tremendous timesaver, I suggest you read this: http://nl2.php.net/manual/en/language.variables.external.php

If your table tbl_alert has an id of some sort, you can use add names to the radio buttons like so:

 <input type="radio" name="selstatus[<?phpecho $row['id'];?>]" ....

... which will give you a nice loopable array in $_POST['selstatus'], with the id of the alert as key, and the value of the radio button as value. Try it and print_r or var_dump $_POST to see the difference. For each of those entries you can run an UPDATE or INSERT query depending on your needs.

Upvotes: 1

Related Questions