Jump to content

[SOLVED] Need little help with radio buttons


Lee-Bartlett

Recommended Posts

I been looking arround on google for a while now, trying some snipets of code etc and none seem to be right, all i want is when the radio button is checked i want it to come up in my mysql database as somthing. not just a 0, problem with it being a 0 is, unticked or ticked = 0, i would like to make it somthing like 1 for yes and 0 for no, can some one help me

 

my basic form so far

 

    <html>
<head> </heaD>
<body>

<form name="form1" method="post" action="">
  <table width="418" align="left" cellpadding="0" cellspacing="0">
    <tr>
      <td width="157"> Name:</td>
      <td width="259"><label for="name"></label>
      <input type="text" name="name" id="name"></td>
    </tr>
    <tr>
      <td>Email:</td>
      <td><label for="email"></label>
      <input type="text" name="email" id="email"></td>
    </tr>
    <tr>
      <td>WiFi Business Name:</td>
      <td><label for="buissnes_name"></label>
      <input type="text" name="buissnes_name" id="buissnes_name_name"></td>
    </tr>
    <tr>
      <td>WiFi Location;</td>
      <td><label for="textfield"></label>
      <input type="text" name="location" id="location"></td>
    </tr>
    <tr>
      <td>Free or Paid:</td>
      <td><input type="radio" name="free" id="free" value="free">
      <label for="radio"></label>
      <input type="radio" name="paid" id="paid" value="paid">
      <label for="radio2"></label></td>
    </tr>
    <tr>
      <td> </td>
      <td><label for="button"></label>
      <input type="submit" name="button" id="button" value="Submit">
      <label for="button2"></label>
      <input type="reset" name="button2" id="button2" value="Reset">       
      <label for="sub"></label></td>
    </tr>
  </table>
</form>

<?php

$sql="INSERT INTO tblbasicform (name, email, buissnes_name, location, freeyes, paidyes)
VALUES
('$_POST[name]','$_POST[email]','$_POST[buissnes_name]','$_POST[location]','$_POST[freeyes]','$_POST[paidyes]')";

if (!mysql_query($sql,$connect))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

Link to comment
Share on other sites

i added a bit at the bottom and tried that, didnt work :( anyone ?

 

<html>
<head> </heaD>
<body>

<form name="form1" method="post" action="">
  <table width="418" align="left" cellpadding="0" cellspacing="0">
    <tr>
      <td width="157"> Name:</td>
      <td width="259"><label for="name"></label>
      <input type="text" name="name" id="name"></td>
    </tr>
    <tr>
      <td>Email:</td>
      <td><label for="email"></label>
      <input type="text" name="email" id="email"></td>
    </tr>
    <tr>
      <td>WiFi Business Name:</td>
      <td><label for="buissnes_name"></label>
      <input type="text" name="buissnes_name" id="buissnes_name_name"></td>
    </tr>
    <tr>
      <td>WiFi Location;</td>
      <td><label for="textfield"></label>
      <input type="text" name="location" id="location"></td>
    </tr>
    <tr>
      <td>Free or Paid:</td>
      <td><input type="radio" name="free" id="free" value="free">
      <label for="radio"></label>
      <input type="radio" name="paid" id="paid" value="paid">
      <label for="radio2"></label></td>
    </tr>
    <tr>
      <td> </td>
      <td><label for="button"></label>
      <input type="submit" name="button" id="button" value="Submit">
      <label for="button2"></label>
      <input type="reset" name="button2" id="button2" value="Reset">       
      <label for="sub"></label></td>
    </tr>
  </table>
</form>

<?php

$sql="INSERT INTO tblbasicform (name, email, buissnes_name, location, freeyes, paidyes)
VALUES
('$_POST[name]','$_POST[email]','$_POST[buissnes_name]','$_POST[location]','$_POST[freeyes]','$_POST[paidyes]')";

if (!mysql_query($sql,$connect))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";




$freeyes_status = 'unchecked';
$paidyes_status = 'unchecked';

if (isset($_POST['Submit1'])) {

$selected_radio = $_POST['freeyes'];

if ($selected_radio = 'freeyes') {
$freeyes_status = 'checked';
}
else if ($selected_radio = 'paidyes') {
$paidyes_status = 'checked';
}
}

mysql_close($connect)
?>

Link to comment
Share on other sites

Are you sure you want to be using radio buttons? Radio buttons only allow you to select one of a given group (one of all with the same name), and once you select it, you can't un-select it. Otherwise, you may want to use checkboxes.

 

Then, the one(s) that are checked would show up in either $_POST['varname'] or $_GET variables after the form is submitted.

Link to comment
Share on other sites

The first thing I see, is that your radio buttons are not a radio group. They are 2 independent radios that can both be checked.

 

I fixed that part and fixed the labels so they actually do what they are supposed to do. Note that I spelled business_name correctly so you may need to change that in your php code. I changed the radio buttons to the name type, so if you look for $_POST['type'] in your code, you should see one or the other. The values are still free or paid.

 

<html>
<head></heaD>
<body>
<form name="form1" method="post" action="">
  <table width="418" align="left" cellpadding="0" cellspacing="0">
    <tr>
      <td width="157"><label for="name">Name:</label></td>
      <td width="259"><input type="text" name="name" id="name"></td>
    </tr>
    <tr>
      <td><label for="email">Email:</label></td>
      <td><input type="text" name="email" id="email"></td>
    </tr>
    <tr>
      <td><label for="business_name">WiFi Business Name:</label></td>
      <td><input type="text" name="business_name" id="business_name"></td>
    </tr>
    <tr>
      <td><label for="location">WiFi Location;</label></td>
      <td><input type="text" name="location" id="location"></td>
    </tr>
    <tr>
      <td>Free or Paid </td>
      <td><label>Free
        <input type="radio" name="type" id="free" value="free">
        </label>
        <label >Paid
        <input type="radio" name="type" id="paid" value="paid">
        </label>
      </td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="button" id="button" value="Submit">
        <input type="reset" name="button2" id="button2" value="Reset">
      </td>
    </tr>
  </table>
</form>

 

Nate

Link to comment
Share on other sites

You should be getting an error with this.

 

$_POST[name]

should be

$_POST['name']

 

You should not take post data and submit it directly to the database. It opens your server up for sql injection attacks.

 

Do it as

 

$name = addslashes($_POST['name']);

 

Turn on error reporting and I think you will find the error I said.

 

Add this to the top of the php page

 

ini_set('error_reporting', E_ALL); 

 

 

Change the $_POST vars to $vars so your query looks like this.

$sql="INSERT INTO tblbasicform (name, email, buissnes_name, location, freeyes, paidyes) VALUES ('$name','$email','$buissnes_name','$location','$freeyes','$paidyes')";

Link to comment
Share on other sites

The ini_set does not help the database insert, it helps you see errors in your code.

 

ini_set('error_reporting', E_ALL ^ E_NOTICE); 
// this will show all errors besides notices which are the undefined vars

try this....

 

echo $sql;

 

then take what that outputs and go to phpmyadmin and run that query there. I am sure you will find errors there.

 

Your mysql_error() should return the same error that phpmyadmin give ya.

 

mysql_query($sql,$connect) or die('Error: ' . mysql_error());

 

 

Change the if statement to the above and tell me if that gives you an error.

 

Nate

Link to comment
Share on other sites

I pretty much ignore notices about undefined variables.

 

$thisVar = 1; 

 

This will produce a Notice:Undefined Variable error, the E_ALL ^ E_NOTICE in the ini_set should show all errors except notices.

 

Show me the $connect part... star out the username pass & host if you wish, and show the mysql_select_db() part.

 

I can't believe though that your not getting anything from mysql_error(), you should either get an error from that or an error from the ini_set that tells you what is wrong.

 

Show the whole script with the $connect pieces in it as well please.

Link to comment
Share on other sites

<?php 
ini_set('error_reporting', E_ALL); 

$db_host = "localhost"; // Your database host server, eg. db.server.com
$db_user = ""; // User who has access to the database
$db_pass = ""; // User password to access database
$db_name = "Roberts_work"; // Existing database name

// -- Connecting to the database (not persistent connection)

$connect = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name,$connect) or die(mysql_error());

Link to comment
Share on other sites

with my current code

 

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\Website\form2.php on line 40

 

with out, i get another radio button and the database doesnt make any entries if i take my old code out

 

if i put ur code before just another radio button, which still doesnt put anything in

Link to comment
Share on other sites

Ok, now we're getting somewhere. If the $connect part of the script and the form and the PHP is all on the same page and you placed the code i gave you last at the bottom, and your getting this error....

 

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\Website\form2.php on line 40

 

Then that means that the connection to the database is failing because the username and/or password your trying to connect with is wrong.

 

In the $connect part, are you defining a username and password in the actual script? I saw that it was blank, but I thought you did that purposely so as not to post your db username and pass on the board.

 

That is all I can tell you for now... Errors are a good thing... at least when something is not working... they tell you what is wrong and you can fix it from there... so define username and password and try it again.

 

Nate

 

 

 

 

Link to comment
Share on other sites

Ok, that means that it connected and read the database and gave the number of tables in the db.

 

Remove any extra code that I gave you now, minus the ini_set part.

 

So, now to the next piece.

 

The DB stuff is *correct*, so we need to look at what is being posted.

 

at the top of the page put this... and post back the results

 

<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>

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.