Jump to content

how can i assign a number value to radio button choices


silverglade

Recommended Posts

i have the code set up so that it rates a friend from very bad to excellent, and puts it in the database. but i want to make the form values a number so i can average it and rate the friend based on a grade from A to F. currently, its just "very bad" or "excellent" stuff like that, stored in the database, here is my code so far.

 

<?php
include("connect1.php");

////////////////////////////////////////
////////////////////////////////////////

/// query db and loop through rows example
$id    = $_POST['id'];
$field2 = $_POST['friend'];
$field3   = $_POST['zip'];
$field4   = $_POST['grade1'];
$field5   = $_POST['grade2'];
$field6   = $_POST['grade3'];
$field7 = $_POST['grade4'];




      
    
    
       mysql_query("INSERT INTO friendgrade (friend,zip,loyalty,courtesy , stability, attitude)VALUES('$field2','$field3','$field4','$field5','$field6' , '$field7')");
      

       


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Update Statement</title>
<style type="text/css">
<!--
.style1 {color: #990000}
.style2 {color: #0066FF}
.style3 {color: #0000CC}
.style4 {color: #993399}
-->
</style>
</head>

<body>

</p>
<div align="center">WELCOME TO THE FRIENDGRADER</div>
<p>Please enter a friend, their zip code, and rate them with the following criteria.<span class="style4"></span><br />
</p>
<form id="form1" name="form1" method="post" action="">

  <table width="90%" border="0">
    
    <tr>
      <td>Friend</td>
      <td>
        <input name="friend" type="text" id="friend" /></td>
    </tr>
    <tr>
      <td width="32%">zip code</td>
      <td width="68%">
       <input name="zip" type="text" id="zip" />
      </td>
    </tr>
    <tr>
      <td>courtesy</td>
      <td><table width="345">
        <tr>
          <td width="337"><label>
            <input type="radio" name="grade1" value="very poor" id="RadioGroup1_0" />
            very poor
            <input type="radio" name="grade1" value="poor" id="RadioGroup1_1" />
poor
<input type="radio" name="grade1" value="ok" id="RadioGroup1_2" />
ok
<input type="radio" name="grade1" value="good" id="RadioGroup1_3" />
good
<input type="radio" name="grade1" value="excellent" id="RadioGroup1_4" />
excellent
</label></td>
        </tr>
       
      </table>
      </td>
    </tr>
   
   
    <tr>
      <td>stability</td>
      <td><input type="radio" name="grade2" id="very_poor3" value="very_poor" />
very poor
  <input type="radio" name="grade2" id="poor3" value="poor" />
poor
<input type="radio" name="grade2" id="ok3" value="ok" />
ok
<input type="radio" name="grade2" id="good3" value="good" />
good
<input type="radio" name="grade2" id="excellent3" value="excellent" />
excellent</td>
    </tr>
    <tr>
      <td>loyalty</td>
      <td><input type="radio" name="grade3" id="very_poor4" value="very_poor" />
very poor
  <input type="radio" name="grade3" id="poor4" value="poor" />
poor
<input type="radio" name="grade3" id="ok4" value="ok" />
ok
<input type="radio" name="grade3" id="good4" value="good" />
good
<input type="radio" name="grade3" id="excellent4" value="excellent" />
excellent</td>
    </tr>
    <tr>
      <td>attitude</td>
      <td><input type="radio" name="grade4" id="very_poor5" value="very_poor" />
very poor
  <input type="radio" name="grade4" id="poor5" value="poor" />
poor
<input type="radio" name="grade4" id="ok5" value="ok" />
ok
<input type="radio" name="grade4" id="good5" value="good" />
good
<input type="radio" name="grade4" id="excellent5" value="excellent" />
excellent</td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
   
</form>


</body>
</html>

Link to comment
Share on other sites

You can change your radio button values by modifying the value attribute, eg

 

            <input type="radio" name="grade1" value="0" id="RadioGroup1_0" />
            very poor
            <input type="radio" name="grade1" value="1" id="RadioGroup1_1" />
poor
<input type="radio" name="grade1" value="2" id="RadioGroup1_2" />
ok
<input type="radio" name="grade1" value="3" id="RadioGroup1_3" />
good
<input type="radio" name="grade1" value="4" id="RadioGroup1_4" />
excellent

Link to comment
Share on other sites

ok i got it to work, but now i want to average the input and grade the "friend" please if anyone can, what is wrong with my code? i just get a blank screen when i load it. any help greatly appreciated. here is my code now.

 

<?php
include("connect1.php");

////////////////////////////////////////
////////////////////////////////////////

/// query db and loop through rows example
$id    = $_POST['id'];
$field2 = $_POST['friend'];
$field3   = $_POST['zip'];
$field4   = $_POST['grade1'];
$field5   = $_POST['grade2'];
$field6   = $_POST['grade3'];
$field7 = $_POST['grade4'];


$field4 = (int)$field4;
$field5 = (int)$field5;
$field6 = (int)$field6;
$field7 = (int)$field7;

      
    
    
       mysql_query("INSERT INTO friendgrade (friend,zip,loyalty,courtesy , stability, attitude)VALUES('$field2','$field3','$field4','$field5','$field6' , '$field7')");
      
$total = 6;
$sum = $field2 + $field3 + $field4 + $field5 + $field6 + $field7;       
$average = $sum/$total;

if ($average <= 1)  
  echo "Your friend has a grade of <strong>F.</strong> ";
  
if ($average <= 2)  
  echo "Your friend has a grade of <strong>D.</strong> ";

if ($average <= 3)  
  echo "Your friend has a grade of <strong>C.</strong> ";

if ($average <= 4)  
  echo "Your friend has a grade of <strong>B.</strong> ";

if ($average <= 5)  
  echo "Your friend has a grade of <strong>A.</strong> ";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Update Statement</title>
<style type="text/css">
<!--
.style1 {color: #990000}
.style2 {color: #0066FF}
.style3 {color: #0000CC}
.style4 {color: #993399}
-->
</style>
</head>

<body>

</p>
<div align="center">WELCOME TO THE FRIENDGRADER</div>
<p>Please enter a friend, their zip code, and rate them with the following criteria.<span class="style4"></span><br />
</p>
<form id="form1" name="form1" method="post" action="">

  <table border="0">
    
    <tr>
      <td>Friend</td>
      <td>
        <input name="friend" type="text" id="friend" /></td>
    </tr>
    <tr>
      <td>zip code</td>
      <td>
       <input name="zip" type="text" id="zip" />
      </td>
    </tr>
    <tr>
      <td>courtesy</td>
      <td><table>
        <tr>
          <td> 
            <input type="radio" name="grade1" value="1" id="RadioGroup1_0" />
            very poor
            <input type="radio" name="grade1" value="2" id="RadioGroup1_1" />
poor
<input type="radio" name="grade1" value="3" id="RadioGroup1_2" />
ok
<input type="radio" name="grade1" value="4" id="RadioGroup1_3" />
good
<input type="radio" name="grade1" value="5" id="RadioGroup1_4" />
excellent
</td>
        </tr>
       
      </table>
      </td>
    </tr>
   
   
    <tr>
      <td>stability</td>
      <td><input type="radio" name="grade2" id="very_poor3" value="1" />
very poor
  <input type="radio" name="grade2" id="poor3" value="2" />
poor
<input type="radio" name="grade2" id="ok3" value="3" />
ok
<input type="radio" name="grade2" id="good3" value="4" />
good
<input type="radio" name="grade2" id="excellent3" value="5" />
excellent</td>
    </tr>
    <tr>
      <td>loyalty</td>
      <td><input type="radio" name="grade3" id="very_poor4" value="1" />
very poor
  <input type="radio" name="grade3" id="poor4" value="2" />
poor
<input type="radio" name="grade3" id="ok4" value="3" />
ok
<input type="radio" name="grade3" id="good4" value="4" />
good
<input type="radio" name="grade3" id="excellent4" value="5" />
excellent</td>
    </tr>
    <tr>
      <td>attitude</td>
      <td><input type="radio" name="grade4" id="very_poor5" value="1" />
very poor
  <input type="radio" name="grade4" id="poor5" value="2" />
poor
<input type="radio" name="grade4" id="ok5" value="3" />
ok
<input type="radio" name="grade4" id="good5" value="4" />
good
<input type="radio" name="grade4" id="excellent5" value="5" />
excellent</td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
   
</form>


</body>
</html>

Link to comment
Share on other sites

i fixed some errors here, but it still shows a blank white screen

 

 

<?php
include("connect1.php");

////////////////////////////////////////
////////////////////////////////////////

/// query db and loop through rows example
$id    = $_POST['id'];
$field2 = $_POST['friend'];
$field3   = $_POST['zip'];
$field4   = $_POST['grade1'];
$field5   = $_POST['grade2'];
$field6   = $_POST['grade3'];
$field7 = $_POST['grade4'];


$field4 = (int)$field4;
$field5 = (int)$field5;
$field6 = (int)$field6;
$field7 = (int)$field7;

      
    
    
       mysql_query("INSERT INTO friendgrade (friend,zip,loyalty,courtesy , stability, attitude)VALUES('$field2','$field3','$field4','$field5','$field6' , '$field7')");
      
$total = 4;
$sum = $field4 + $field5 + $field6 + $field7;       
$average = $sum/$total;

if ($average <= 1)  
  echo "Your friend has a grade of <strong>F.</strong> ";
  
if ($average <= 2)  
  echo "Your friend has a grade of <strong>D.</strong> ";

if ($average <= 3)  
  echo "Your friend has a grade of <strong>C.</strong> ";

if ($average <= 4)  
  echo "Your friend has a grade of <strong>B.</strong> ";

if ($average <= 5)  
  echo "Your friend has a grade of <strong>A.</strong> ";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Update Statement</title>
<style type="text/css">
<!--
.style1 {color: #990000}
.style2 {color: #0066FF}
.style3 {color: #0000CC}
.style4 {color: #993399}
-->
</style>
</head>

<body>

</p>
<div align="center">WELCOME TO THE FRIENDGRADER</div>
<p>Please enter a friend, their zip code, and rate them with the following criteria.<span class="style4"></span><br />
</p>
<form id="form1" name="form1" method="post" action="">

  <table border="0">
    
    <tr>
      <td>Friend</td>
      <td>
        <input name="friend" type="text" id="friend" /></td>
    </tr>
    <tr>
      <td>zip code</td>
      <td>
       <input name="zip" type="text" id="zip" />
      </td>
    </tr>
    <tr>
      <td>courtesy</td>
      <td><table>
        <tr>
          <td> 
            <input type="radio" name="grade1" value="1" id="RadioGroup1_0" />
            very poor
            <input type="radio" name="grade1" value="2" id="RadioGroup1_1" />
poor
<input type="radio" name="grade1" value="3" id="RadioGroup1_2" />
ok
<input type="radio" name="grade1" value="4" id="RadioGroup1_3" />
good
<input type="radio" name="grade1" value="5" id="RadioGroup1_4" />
excellent
</td>
        </tr>
       
      </table>
      </td>
    </tr>
   
   
    <tr>
      <td>stability</td>
      <td><input type="radio" name="grade2" id="very_poor3" value="1" />
very poor
  <input type="radio" name="grade2" id="poor3" value="2" />
poor
<input type="radio" name="grade2" id="ok3" value="3" />
ok
<input type="radio" name="grade2" id="good3" value="4" />
good
<input type="radio" name="grade2" id="excellent3" value="5" />
excellent</td>
    </tr>
    <tr>
      <td>loyalty</td>
      <td><input type="radio" name="grade3" id="very_poor4" value="1" />
very poor
  <input type="radio" name="grade3" id="poor4" value="2" />
poor
<input type="radio" name="grade3" id="ok4" value="3" />
ok
<input type="radio" name="grade3" id="good4" value="4" />
good
<input type="radio" name="grade3" id="excellent4" value="5" />
excellent</td>
    </tr>
    <tr>
      <td>attitude</td>
      <td><input type="radio" name="grade4" id="very_poor5" value="1" />
very poor
  <input type="radio" name="grade4" id="poor5" value="2" />
poor
<input type="radio" name="grade4" id="ok5" value="3" />
ok
<input type="radio" name="grade4" id="good5" value="4" />
good
<input type="radio" name="grade4" id="excellent5" value="5" />
excellent</td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
   
</form>


</body>
</html>

Link to comment
Share on other sites

Please don't post PHP to the HTML, Javascript and CSS sections of the forum. PHP is irrelevant to these three things. All that is relevant is the output of your PHP script. So please run the script, copy the source, format it so it's readable, and paste it here.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.