• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 530
  • Last Modified:

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.
0
paradeshi
Asked:
paradeshi
  • 4
  • 2
1 Solution
 
wuyinzhiCommented:
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

0
 
paradeshiAuthor Commented:
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!
0
 
wuyinzhiCommented:
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


0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
paradeshiAuthor Commented:
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!
0
 
wuyinzhiCommented:
i have tried the code and everything is ok, make sure you already put this codes in top of pray.php:

mysql_connect("localhost", "db_user", "db_pass") or die(mysql_error());
 mysql_select_db("db_name") or die(mysql_error());

i think the error message you've got is related with database issue, can you try simple code like this:
 
<?
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("dbmusic") or die(mysql_error());
$query = "SELECT sunday FROM `data` where sunday = 'test'";
$result = mysql_query($query);	
if ($row = mysql_fetch_array($result, MYSQL_NUM)) 	
       	echo " test";   		
	
echo "no error";
?>

Open in new window


i've tried on my side it's ok, but if same error occurs on your side, maybe there's something wrong with OS or mySQL  installation or configuration.
0
 
wuyinzhiCommented:
oops sorry forgot to change db_user, pass, and db name, please adjust with yours :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now