Link to home
Start Free TrialLog in
Avatar of paradeshi
paradeshi

asked on

Disable Radio Button Once Data Is Sent To MySQL Database

Let's say I have a form to fill which needs to go into a MySQL database.

Name:
Email:

It's pretty basic in nature until it gets here. Here I want people to select a timeslot that gets entered into the database but once a timeslot is selected the next person filling out the form sees the radio button disabled or grayed out because that timeslot is already taken.

For example, here is my code for "process.php" which holds all the database user information and the variables.

<?
 $name=$_POST['name'];
 $email=$_POST['email'];
 $sunday17=$_POST['sunday17'];
 $monday18=$_POST['monday18'];
 mysql_connect("localhost", "db_user", "db_pass") or die(mysql_error());
 mysql_select_db("db_name") or die(mysql_error());
 mysql_query("INSERT INTO `data` VALUES ('$name', '$email', '$sunday17', '$monday18')");
 Print "Thank you for choosing the timeslot!";
?>

------------------------------------------------------------------------------------------------------------------------------------------

And then I have the actual HTML code in "form.php":

<form action="process.php" method="post">
<table width="100%" border="0" cellspacing="2" cellpadding="3">
  <tr>
    <td>
        <label for="name">Full Name:</label>
        <input type="text" name="name" id="full_name" />
    </td>
  </tr>
  <tr>
    <td><label for="email">Email Address:</label>
        <input type="text" name="email" id="email" />
    </td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>Sunday:</td>
        <td>Monday:</td>
      </tr>
      <tr>
        <td>
            <label><input type="radio" name="sunday17" id="sunday17" value="Sunday 12:00 AM" />12:00 AM</label>
            <label><input type="radio" name="sunday17" id="sunday17" value="Sunday 12:15 AM" />12:15 AM</label>
            <label><input type="radio" name="sunday17" id="sunday17" value="Sunday 12:30 AM" />12:30 AM</label>
            <label><input type="radio" name="sunday17" id="sunday17" value="Sunday 12:45 AM" />12:45 AM</label></td>
        <td>
              <label><input type="radio" name="monday18" id="monday18" value="Monday 12:00 AM" />12:00 AM</label>
            <label><input type="radio" name="monday18" id="monday18" value="Monday 12:15 AM" />12:15 AM</label>
            <label><input type="radio" name="monday18" id="monday18" value="Monday 12:30 AM" />12:30 AM</label>
            <label><input type="radio" name="monday18" id="monday18" value="Monday 12:45 AM" />12:45 AM</label></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><input type="submit" id="submit" name="submit" value="Submit" />
      <input type="reset" id="reset" name="reset" value="Reset" /></td>
  </tr>
</table>
</form>

Any help is highly appreciated. Thank you!

Basically I want people to enter their name, email and choose a 15 minute timeslot everyday of the week for seven days but when someone chooses for example 12:30 AM then it needs to be grayed out.
Avatar of wuyinzhi
wuyinzhi
Flag of Indonesia image

Hi, maybe you can try something like this:

<form action="process.php" method="post">
<table width="100%" border="0" cellspacing="2" cellpadding="3">
  <tr>
    <td>
        <label for="name">Full Name:</label>
        <input type="text" name="name" id="full_name" />
    </td>
  </tr>
  <tr>
    <td><label for="email">Email Address:</label>
        <input type="text" name="email" id="email" />
    </td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>Sunday:</td>
        <td>Monday:</td>
      </tr>
      <tr>
        <td>
            <label><input type="radio" name="sunday17" id="sunday17" value="Sunday 12:00 AM" <?=check("sunday", "Sunday 12:10 AM")?>/>12:00 AM</label>
            <label><input type="radio" name="sunday17" id="sunday17" value="Sunday 12:15 AM" <?=check("sunday", "Sunday 12:15 AM")?>/>12:15 AM</label>
            <label><input type="radio" name="sunday17" id="sunday17" value="Sunday 12:30 AM" <?=check("sunday", "Sunday 12:30 AM")?>/>12:30 AM</label>
            <label><input type="radio" name="sunday17" id="sunday17" value="Sunday 12:45 AM" <?=check("sunday", "Sunday 12:45 AM")?>/>12:45 AM</label></td>
        <td>
            <label><input type="radio" name="monday18" id="monday18" value="Monday 12:00 AM" <?=check("monday", "Monday 12:00 AM")?>/>12:00 AM</label>
            <label><input type="radio" name="monday18" id="monday18" value="Monday 12:15 AM" <?=check("monday", "Monday 12:15 AM")?>/>12:15 AM</label>
            <label><input type="radio" name="monday18" id="monday18" value="Monday 12:30 AM" <?=check("monday", "Monday 12:30 AM")?>/>12:30 AM</label>
            <label><input type="radio" name="monday18" id="monday18" value="Monday 12:45 AM" <?=check("monday", "Monday 12:45 AM")?>/>12:45 AM</label></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><input type="submit" id="submit" name="submit" value="Submit" />
      <input type="reset" id="reset" name="reset" value="Reset" /></td>
  </tr>
</table>
</form>

<?
function check($field, $value)
{
  $query = "SELECT $field FROM `data` where $field = '$value'";
  $result = mysql_query($query);	
  if ($row = mysql_fetch_array($result, MYSQL_NUM)) 	
		echo " disabled";   
		
}
?>

Open in new window

Avatar of paradeshi
paradeshi

ASKER

I think I am close. Thank you very much. Just a  few questions.

In this line  
<label><input type="radio" name="sunday17" id="sunday17" value="Sunday 12:00 AM" <?=check("sunday", "Sunday 12:10 AM")?>/>12:00 AM</label>

Open in new window


Is  "sunday" a column in the database called 'data'? The reason I ask that is that my sql right now looks like this:

 
CREATE TABLE `data` (
  `name` varchar(30) NOT NULL DEFAULT '',
  `email` varchar(30) DEFAULT NULL,
  `sunday17` varchar(50) DEFAULT 'NULL ',
  `monday18` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `data`
--

INSERT INTO `data` (`name`, `email`, `sunday17`, `monday18`) VALUES
('First Last', 'johndoe@doesnotexist.tv', 'Sunday 12:15 AM', ''),
('First1 Last1', 'johndoe@doesnotexist.tv', '', 'Monday 12:15 AM'),
('First2 Last2', 'johndoe@doesnotexist.tv', 'Sunday 12:00 AM', '');

Open in new window



If "sunday" or "monday is a column then I would have to change "sunday17" and "monday18" to just "sunday and "monday".

And how would I process the data using process.php if my code looks like this:

 
<?
 $name=$_POST['name'];
 $email=$_POST['email'];
 $sunday17=$_POST['sunday17'];
 $monday18=$_POST['monday18'];
 mysql_connect("localhost", "db_user", "db_pass") or die(mysql_error());
 mysql_select_db("db_name") or die(mysql_error());
 mysql_query("INSERT INTO `data` VALUES ('$name', '$email', '$sunday17', '$monday18')");
 Print "Thank you for taking the time to pray!";
?>

Open in new window


Would I have to change process.php as well? Thank you!
yes that's right. "sunday" is name of column/field of table "data". If you want to leave the column name as "sunday17", then you should change the code to something like this:

<label><input type="radio" name="sunday17" id="sunday17" value="Sunday 12:00 AM" <?=check("sunday17", "Sunday 12:10 AM")?>/>12:00 AM</label>

Open in new window


So am I correct with the rest of the code with MySQL query, process.php etc? I am getting an error when I implement the above code. The error is:

 
Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /nfs/c01/h09/tng/123456/domains/domains.com/html/pray.php on line 45

Open in new window


And line 45 is

 
41    <?
42    function check($field, $value)
43    {
44       $query = "SELECT $field FROM `data` where $field = '$value'";
45       $result = mysql_query($query);	
46       if ($row = mysql_fetch_array($result, MYSQL_NUM)) 	
47        	echo " disabled";   
48		
49    }
50    ?>

Open in new window


Thank you!
ASKER CERTIFIED SOLUTION
Avatar of wuyinzhi
wuyinzhi
Flag of Indonesia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oops sorry forgot to change db_user, pass, and db name, please adjust with yours :)