将PHP表单中的单选格值提交到MySQL表(更新表中的值)

时间:2010-06-04 00:30:15

标签: php mysql

我目前正在使用jQuery iPhone风格的无线电(在http://devgrow.com/iphone-style-switches/处找到)构建一个php表单。默认情况下,MySQL表中的每个新条目的无线电值都设置为NO,但我提交表单时遇到了困难。

表单如何能够告诉单选按钮的当前状态(即提交时是否设置为是或否)?我还需要使用什么MySQL代码来更新表中的值?这是我到目前为止的代码。

PHP表单代码

<!--- 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" 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'];

    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>

提交表单的代码

      <?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;

                };  
                ?>

1 个答案:

答案 0 :(得分:3)

该信息将出现在$_POST['<id>']<id>对应于您在php表单中给出的tbl_alerts中的行:

echo'&lt; input type =“radio”id =“radio1” name =“'。$ rows ['id']。'” value =“no”checked /&gt; &lt; input type =“radio”id =“radio2” name =“'。$ rows ['id']。'” value =“yes”/&gt;

(强调补充)

您可以(并且应该)通过在处理脚本的顶部添加var_dump($ _ POST)来验证。

您的处理脚本很难知道id值,但您可能想要的是将收音机命名为其他名称,然后添加一个隐藏的表单字段来存储ID。

离。您的单选按钮会出现:

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

然后在你的代码中的某个地方:

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

这样,单选按钮值将在$_POST['status']中,行的ID将在$_POST['statusid']