1 Replies - 2319 Views - Last Post: 03 June 2010 - 08:35 PM Rate Topic: -----

#1 jeansymolanza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 20-February 08

submitting radio values in a php form to a mysql table

Posted 03 June 2010 - 05:46 PM

i am currently building a php form utilising the jquery iphone style radios (found at http://devgrow.com/i...yle-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. i would appreciate any help. much love and GOD bless.

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


code to submit form (in progress)

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

                };  
                ?>



Is This A Good Question/Topic? 0
  • +

Replies To: submitting radio values in a php form to a mysql table

#2 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: submitting radio values in a php form to a mysql table

Posted 03 June 2010 - 08:35 PM

You can try this to show the current status of radio button-

$status = $rows['status'];

echo "<input type='radio' name='status' value='$status' checked>$status<br/>";

if($status=="NO")
{
	echo "<input type='radio' name='status' value='YES'>YES<br/>";
}

else
{
	echo "<input type='radio' name='status' value='NO'>NO<br/>";
}

echo "<input type='submit' value='Update'>";



Then in the processing part add this code to update the status-
$query="UPDATE tbl_alert SET status='$new_status' WHERE id='$id'";
$result=mysql_query($query)
or die(mysql_error());


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1