Page 1 of 1

checkbox problem

Posted: Fri Aug 11, 2006 6:46 am
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

Posted: Fri Aug 11, 2006 7:05 am
by volka
May we see some code?

Posted: Fri Aug 11, 2006 7:49 am
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.

Posted: Fri Aug 11, 2006 8:03 am
by JayBird
jamiel wrote:Take out the value attribute.
That isn't exactly a "solution" Image

Posted: Fri Aug 11, 2006 8:20 am
by jamiel
Yeah, then let's see some code.

Posted: Fri Aug 11, 2006 9:36 am
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]

Posted: Fri Aug 11, 2006 9:47 am
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');
?>