checkbox problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjaywoods
Forum Newbie
Posts: 11
Joined: Wed Jul 12, 2006 9:32 am

checkbox problem

Post by mjaywoods »

Hi this is most prolly something very simply but i am having problems with checkboxes and getting the right info into mysql


I have a form with

Code: Select all

<input name="terms" type="checkbox" value="yes" >
but when i try to put the info into mysql with the checkbox not checked instead of a blank field its still adding the value "yes". Im using $_POST["terms"] to get the value frm the form.

can you help?

please
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

May we see some code?
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Take out the value attribute.

In your code use :

Code: Select all

$_POST['terms'] = ($_POST['terms']) ? 'yes' : 'no';
Just before inserting into the database.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

jamiel wrote:Take out the value attribute.
That isn't exactly a "solution" Image
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Yeah, then let's see some code.
mjaywoods
Forum Newbie
Posts: 11
Joined: Wed Jul 12, 2006 9:32 am

Post by mjaywoods »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok heres some code



page1.php

[syntax="html"]
<form  action="nextpage.php" method="post">
<input name="terms" type="checkbox" value="yes">
<input type="submit" name="button" value="Submit">
</form>
nextpage.php[/syntax]

Code: Select all

<?php

$terms = $_POST['terms'];

$DBhost = "#####";
$DBuser = "#####";
$DBpass = "#####";
$DBName = "#####";

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

@mysql_select_db("$DBName") or die("Unable to select database $DBName");


$sqlquery = "INSERT INTO project_users (Terms)
VALUES('$terms')";

$results = mysql_query($sqlquery);

mysql_close();

header('location: http://page1.php');
Ive shortened my code but you should get the jist now

I need the checkbox to put no in the database if the checbox isn't checked and yes if it is


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

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

$DBhost = "#####";
$DBuser = "#####";
$DBpass = "#####";
$DBName = "#####";

$db = mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
@mysql_select_db($DBName, $db) or die("Unable to select database $DBName");

$terms = isset($_POST['terms']) ? 'yes':'no';
$sqlquery = "INSERT INTO
		project_users (Terms)
	VALUES
		('$terms')";
$results = mysql_query($sqlquery, $db) or die(mysql_error());
header('location: http://page1.php');
?>
Post Reply