insert data using radio button

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
celcomgirl
php-forum Active User
php-forum Active User
Posts: 29
Joined: Tue Sep 02, 2008 7:16 pm

Thu Nov 20, 2008 6:14 pm

i... i want insert data using radio button but it not succes...im not familiar insert data using radio button.here some coding that i have made.

Code: Select all

<?php
include 'includes/db_connect.php';
include 'includes/css.php';
if (isset($_POST['bttnAdd']))
{
	$id = $_POST['id'];
	$name = $_POST['name'];
	$dest = $_POST['dest'];

	
	$data = 1;
	$query = "INSERT INTO artis(name, dest)
			  VALUES('$name','$dest')";
	mysql_query($query) or die('Query failed. ' . mysql_error());
	
	echo "<script language='JavaScript'> alert('The record is successfully added');</script>";
	echo "<script language='JavaScript'> window.location ='add_dest.php';</script>";
}
?>

<html>
<head>
<title>MEC</title>

<script type="text/javascript">
function validDate(add_dest)
{
	var prob=false;
	
	if(add_dest.name.value=="")
	{
		alert("Please enter name");
		prob=true;
	}
	
	if(add_dest.dest.value=="")
	{
		alert("Please choose dest");
		prob=true;
	}
	
	if(prob)
	{			
		return false;
	}
	
	else
	{
		return true;
	}
}
</script>
<body>
<form name="add_dest" action="rodiobttn.php" onSubmit="return validDate(this);">
Name: <input name = "name" maxlength = "100" size = "50"><br>


  <b>Destination</b> 
    <p>
      <input type="radio" name="dest" value="domestic" <?php if(isset($_POST['dest']) && ($_POST['dest'] == 'domestic') )echo ' checked="checked"'; ?>/>
      Domestic => 
    	  <select name="dest">
          <option value="" selected>Choose Destination 
          <option value="SZB">SZB-Shah Alam 
          <option value="MEL">MEL-Melaka 
          <option value="KCH">KCH-Kuching 
          <option value="KTN">KTN-Kuantan </select>
    </p>
	
	<input type="radio" name="dest" value="international" <?php if(isset($_POST['dest']) && ($_POST['dest'] == 'international') )echo ' checked="checked"'; ?>/>
  International => 
  <select name="select">
    <option value="" selected>Choose Destination 
    <option value="JHB">JHB-Johor Bahru 
    <option value="BKI">BKI-Sabah 
    <option value="SBW">SBW-Sibu 
    <option value="MYY">MYY-Miri 
    <option value="KBR">KBR-Kota Bahru 
    <option value="IPH">IPH-Ipoh 
    <option value="AOR">AOR-Alor Setar 
    <option value="TGG">TGG-Terengganu 
    <option value="PEN">PEN-Pulau Pinang 
    <option value="BTL">BTL-Bintulu 
    <option value="SARIKEI">SARIKEI 
  </select>
  </p> 
  <input type="submit" name="bttnAdd" value="SUBMIT"/>
</form>
</body>
</html>
when i press button submit, it do not show error information but it not sucsess. why ??? plz help me..... ??? ??? :help: :help:
User avatar
gesf
Moderator
Moderator
Posts: 1716
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Mon Nov 24, 2008 7:39 am

Hey celcomgirl,

Let's do a thing first. Put this next code at the very top of your code, so you can be notified about every little error...

Code: Select all

error_reporting(E_ALL);
ini_set("display_errors", true);
celcomgirl
php-forum Active User
php-forum Active User
Posts: 29
Joined: Tue Sep 02, 2008 7:16 pm

Mon Nov 24, 2008 4:42 pm

gesf wrote:Hey celcomgirl,

Let's do a thing first. Put this next code at the very top of your code, so you can be notified about every little error...

Code: Select all

error_reporting(E_ALL);
ini_set("display_errors", true);
thank u gesf for this info...i have modified little bit at my source code.but it not working successfully. where is when i select domestic, the destination cannot be insert into database but when i select international it can be insert into database. after i put code that u gave.no error found. here is my code:

Code: Select all

<?php
include 'includes/db_connect.php';
include 'includes/css.php';
if (isset($_POST['bttnAdd']))
{
	//$id = $_POST['id'];
	$name = $_POST['name'];
	$dest = $_POST['dest'];
	
	$data = 1;
	$query = "INSERT INTO artis(name, dest)
			  VALUES('$name','$dest')";
	mysql_query($query) or die('Query failed. ' . mysql_error());
	
	echo "<script language='JavaScript'> alert('The record is successfully added');</script>";
	echo "<script language='JavaScript'> window.location ='rodiobttn4.php';</script>";
}
?>

<html>
<head>
<title>Radiobutton4</title>

<script type="text/javascript">
function validDate(add_dest)
{
	var prob=false;
	
	if(add_dest.name.value=="")
	{
		alert("Please enter name");
		prob=true;
	}
	
	if(prob)
	{			
		return false;
	}
	
	else
	{
		return true;
	}
}
</script>
<body>
<form name="add_dest" action="rodiobttn4.php" method='post' onSubmit="return validDate(this);">
Name: <input name = "name" maxlength = "100" size = "50"><br>

  <b>Destination</b> 
    <p>
      <input type="radio" name="domestic" value="domestic" <?php if(isset($_POST['dest']) && ($_POST['dest'] == 'domestic') )echo ' checked="checked"'; ?>/>
      Domestic => 
    	  <select name="dest">
          <option value="" selected>Choose Destination</option> 
          <option value="SZB">SZB-Shah Alam</option>
          <option value="MEL">MEL-Melaka</option> 
          <option value="KCH">KCH-Kuching</option> 
          <option value="KTN">KTN-Kuantan</option> </select>
    </p>
	
  <input type="radio" name="international" value="international" <?php if(isset($_POST['dest']) && ($_POST['dest'] == 'international') )echo ' checked="checked"'; ?>/>
  International => 
  <select name="dest">
    <option value="" selected>Choose Destination</option> 
    <option value="JHB">JHB-Johor Bahru</option>
    <option value="BKI">BKI-Sabah</option>
    <option value="SBW">SBW-Sibu</option> 
    <option value="MYY">MYY-Miri</option>
    <option value="KBR">KBR-Kota Bahru</option>
    <option value="IPH">IPH-Ipoh</option>
    <option value="AOR">AOR-Alor Setar</option>
    <option value="TGG">TGG-Terengganu</option>
    <option value="PEN">PEN-Pulau Pinang</option>
    <option value="BTL">BTL-Bintulu</option>
    <option value="SARIKEI">SARIKEI</option>
  </select>
  </p> 
  <input type="submit" name="bttnAdd" value="SUBMIT"/>
</form>
</body>
</html>
User avatar
gesf
Moderator
Moderator
Posts: 1716
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Tue Jun 09, 2009 7:32 am

Hey celcomgirl,

Now i see the problem. The radio buttons must have the same name, like:

Code: Select all

<span>International Destination ?</span>
<input type="radio" name="dest" value="Yes" />
<input type="radio" name="dest" value="No" />
and than you'll have Yes or No inside your $_POST['dest'];
Post Reply