Jump to content


Photo

PHP checkbox


  • Please log in to reply
10 replies to this topic

#1 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 13 October 2006 - 05:13 PM

I am working on a PM system for my site, and having trouble with a line of code for a checkbox. I want it so that if the ncheckbox is checked, it inserts the word "yes" in to the column "sig" in my mysql database. hers the code I have so far. It gives me
Parse error: parse error in /Library/WebServer/Documents/users/send.php on line 8

send.php:
<?php
session_start();
$con = mysql_connect("localhost","ZackBabtkis","");
$subject = mysql_real_escape_string($_POST[subject]);
$message = mysql_real_escape_string($_POST[message]);
$user = $_COOKIE["user"];
$sender = $_SESSION['username'];
$date = date("F j, Y, g:i a");
$sig = if (isset($_POST["sig"])) echo "yes"; 
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("test", $con);
$sql = "INSERT INTO messages (Username, Sender, Subject, Message, Date, sig) VALUES ('$user','$sender','$subject','$message', '$date', '$sig')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Your message has been sent to $user";
mysql_close($con)
?>


#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 13 October 2006 - 05:15 PM

$sig = (isset($_POST["sig"])) ? "yes" : NULL;
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 13 October 2006 - 05:18 PM

okay, but how do I make it so that when the box isnt checked it insterts "no" in to the database?

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 13 October 2006 - 05:23 PM

change the NULL to "no"

$sig = (isset($_POST["sig"])) ? "yes" : "no";

or else in your database you can set the default to "no".
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 13 October 2006 - 05:25 PM

Small modification (looks nicer and is smaller):

<?php
session_start();
$con = mysql_connect("localhost","ZackBabtkis","") or die('Could not connect: ' . mysql_error());
mysql_select_db("test", $con);

$subject = mysql_real_escape_string($_POST['subject']);
$message = mysql_real_escape_string($_POST['message']);

$date = date("F j, Y, g:i a");

$sig = isset($_POST['sig']) ? "yes" : "no";

mysql_query("INSERT INTO messages (Username, Sender, Subject, Message, Date, sig) VALUES ('{$_COOKIE['user']}','{$_SESSION['username']}','{$subject}','{$message}', '{$date}', '{$sig}')",$con)) or die('Error: ' . mysql_error());

echo "Your message has been sent to {$user}";

mysql_close($con)
?>


#6 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 13 October 2006 - 05:30 PM

when I have
$sig = (isset($_POST["sig"])) ? "yes" : "no";
it only echos no, when I change it to
$sig = (isset($_POST["sig"])) ? "no" : "yes";
it always inserts yes. it seems to be ignoring the first quotes

#7 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 13 October 2006 - 05:33 PM

Make sure that your checkbox has a non-empty value attribute.

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 13 October 2006 - 05:33 PM

then the problem is with your post variable itself

$sig = (isset($_POST["sig"])) ? "yes" : "no";

basically what this does is if $_POST['sig'] is set (it exists) then $sig = "yes";  if it does not exist, then $sig = "no";

the fact that it keeps setting $sig to the one on the right of the : means taht $_POST['sig'] is not set.  check to make sure you actually named your checkbox 'sig' in your form, and that it is spelled right, your form method = 'post' etc..
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 13 October 2006 - 06:01 PM

thanks, i figured it out. one more question though. I am trying to recieve the message ont eh other end, and if the column 'sig' says yes, I want the message to echo test on the bottom. this is what I have so far, but it is giving me a parse error.
<?php

$con = mysql_connect("localhost","ZackBabtkis","") or die('Could not connect: ' . mysql_error());
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM messages WHERE ID=" . $_GET['id']);

while($row = mysql_fetch_array($result))
{
$sigtest = $row['sig'];
$sig = if ($sigtest = "no") echo "test";
        
         echo $sig
?>
can someone tell me what might be wrong? I appreciate all the quick helpful replies by the way :)

#10 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 13 October 2006 - 09:07 PM

You need a semilcolon at the end of that last echo.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#11 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 14 October 2006 - 06:41 AM

a) once again you have not properly used the ternary operator. The ternary operator format looks like this:

$variable = (condition) ? condition_is_true : condition_is_false;

b) you are also using an = instead of an == sign in your condition. Those are 2 different operators.  = is the assignment operator. == is the equality operator. 

c) you should NEVER insert a $_GET variable directly into a query, otherwise you are begging for sql injection hacks. you should always sanitize your variables before using them in queries. you should at the very very VERY least do something like what I have done below.

d) as mentioned above, you're missing a ; on your echo $sig line.

<?php

$con = mysql_connect("localhost","ZackBabtkis","") or die('Could not connect: ' . mysql_error());
mysql_select_db("test", $con);
$id = mysql_real_escape_string($_GET['id']);
$result = mysql_query("SELECT * FROM messages WHERE ID=" . $id);

while($row = mysql_fetch_array($result)) {
   $sigtest = $row['sig'];
   $sig = ($sigtest == "no") "test" : NULL; // will set $sig to "test" if $sigtest equals "no"
   echo $sig;
}
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users