开发者

Submitting radio values in a PHP form to a MySQL table (updating value in table)

I am currently building a php form utilising the jQuery iPhone style radios (found at http://devgrow.com/iphone-style-switches/). By default the radio value is set to NO in the MySQL table for every new entry, but I am having difficulty submitting the form.

How will the form be able to tell the current status of the radio button (i.e. is it set to yes or no upon submission)? Also what MySQL code would I need to use to update the value in the table? This is the code I have so far.

PHP form code

<!--- iphone checkbox --->

<script type="text/javascript"> 
$(document).ready( function(){ 
    $(".cb-enable").click(function(){
        var parent = $(this).parents('.switch');
        $('.cb-disable',parent).removeClass('selected');
        $(this).addClass('selected');
        $('.checkbox',parent).attr('checked', true);
    });
    $(".cb-disable").click(function(){
        var parent = $(this).parents('.switch');
        $('.cb-enable',parent).removeClass('selected');
        $(this).addClass('selected');
        $('.checkbox',parent).attr('checked', false);
    });
});
</script> 

 <style type="text/css"> 
.cb-enable, .cb-disable, .cb-enable span, .cb-disable span { background: url(resources/switch.gif) repeat-x; display: block; float: left; }
    .cb-enable span, .cb-disable span { line-height: 30px; display: block; background-repeat: no-repeat; font-weight: bold; }
    .cb-enable span { background-position: left -90px; padding: 0 10px; }
    .cb-disable span { background-position: right -180px;padding: 0 10px; }
    .cb-disable.selected { background-position: 0 -30px; }
    .cb-disable.selected span { background-position: right -210px; color: #fff; }
    .cb-enable.selected { background-position: 0 -60px; }
    .cb-enable.selected span { background-position: left -150px; color: #fff; }
    .switch label { cursor: pointer; }
    .switch input { display: none; }
</style> 


</head>

<body style="text-align:left;">

<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" alig开发者_运维知识库n="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'];

    if ( $status == yes ) {

    echo '<input type="radio" id="radio1" name="'.$rows['id'].'" value="no" checked/>
    <input type="radio" id="radio2" name="'.$rows['id'].'" value="yes"/>
    <label for="radio1" class="cb-enable selected"><span>Yes</span></label>
    <label for="radio2" class="cb-disable"><span>No</span></label>';

} else {

    echo '<input type="radio" id="radio1" name="'.$rows['id'].'" value="yes"/>
    <input type="radio" id="radio2" name="'.$rows['id'].'" value="no" checked/>
    <label for="radio1" class="cb-enable"><span>Yes</span></label>
    <label for="radio2" class="cb-disable selected"><span>No</span></label>';

} 

?>
    </p>
    </div></td>
  </tr>
  <tr>
    <td></td>
    <td align="center"><div style="padding-left: 30px;">
<button type="submit">Update</button>
        <input type="hidden" name="notc2" value="1"/>
    </div></td>
  </tr>
  <?php
    }
    }
    ?>
</table>
</form>


</div>

code to submit form

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

            exit;

                };  
                ?>


The information will be present within $_POST['<id>'] where <id> corresponds to the row in tbl_alerts as given in your php form:

echo '<input type="radio" id="radio1" name="'.$rows['id'].'" value="no" checked/> <input type="radio" id="radio2" name="'.$rows['id'].'" value="yes"/>

(emphasis added)

You can (and should) verify that by adding a var_dump($_POST) up the top of your processing script.

It will be hard for your processing script to know the id value though so what you probably want is name the radio as some other name, and then add a hidden form field that will store the id.

ex. your radio buttons would appear:

<input type="radio" id="radio1" name="status" value="no" checked/>
<input type="radio" id="radio2" name="status" value="yes"/>

and then somewhere in your code you have:

echo '<input type="hidden" name="statusid" value="'.$rows['id'].'"/>';

That way, the radio button value will be in $_POST['status'] and the id of the row will be in $_POST['statusid']

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜