Jump to content


Photo

Checkbox echo issue


  • Please log in to reply
17 replies to this topic

#1 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 21 August 2006 - 10:22 AM

Hi,

I have checkboxes which I need a user to be able to update, the issue is that when you load the page the checkboxes are not echoing the results from the database. I have the following code which is the html:

<p>Beautician:</td><td><input type="checkbox" name="beautician" value="<?php if($test1 == "on"){echo" CHECKED";}?>
</p>

The value that checks for 'on' is actually in the page that receives the result from the above code:

if (isset($_POST['test1']) == "on")
    $test1 = "true";  //  I don't know what $test1 is supposed to be, change as needed.
  else
    $test1= "null";
[/code]

So the question is how can I echo these results?

Thanks,

G

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 August 2006 - 10:28 AM

For a start there's no closing tag on the <Input> tag...

Try this:

<p>Beautician:</td><td><input type="checkbox" name="beautician" value="<?php if($test1 == "on"){echo" CHECKED";}?>">
</p>

Also although you've only posted a snippet of your code here, it looks as though your HTML tags are nested incorrectly.  The first tag in a paragraph tag should not be a closing table tag as in your example "<p></td>".

Maybe try this:

<p>
  Beautician:
  <td>
    <input type="checkbox" name="beautician" value="<?php if($test1 == "on"){echo" CHECKED";}?>">
  </td>
</p>

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 21 August 2006 - 10:41 AM

Thanks, yes I have only posted a snippet of the html. I have edited it though with your suggestion, but still nothing. Should I move:

if (isset($_POST['test1']) == "on")
    $test1 = "true";  //  I don't know what $test1 is supposed to be, change as needed.
  else
    $test1= "null";

from the results page to the page that contains the html? But then when you submit the page it runs the above routine anyway, oh I am confused now..

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 August 2006 - 10:49 AM

I'm assuming this code was taken from somewhere else.

Are you able to post the full code of the two pages so I can get a better look?

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 10:51 AM

ok have a look at your source... Is CHECKED being echoed out?

If not then there is something amiss in the code if it IS then...

swap echo " CHECKED" for echo " checked=\"checked\"";

and DO make sure the input tag is closed
follow me on twitter @PHPsycho

#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 August 2006 - 10:59 AM

swap echo " CHECKED" for echo " checked=\"checked\"";


I can see what genista's done here.  HTML will allow an input type of 'checkbox' to be checked with just CHECKED in the tag, it doesn't require a descriptor of checked="checked" but it certainly would help here.

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 11:08 AM

Yeah I know but I just like standards - if the doctype is strict then I think just CHECKED will fail!
follow me on twitter @PHPsycho

#8 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 August 2006 - 11:10 AM

Yeah I know but I just like standards


Someone after my own heart  8)
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#9 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 11:22 AM

hugss n kisses ;)
follow me on twitter @PHPsycho

#10 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 21 August 2006 - 11:52 AM

Ok I have done that but still nothing, here are the two pages:

updatedetails.php
$id = $_SESSION['username'];
$query = "select * from suppliers where username='$id'";

    //now we pass the query to the database 
	$result=mysql_query($query, $link) or die("MySQL query $query failed.  Error if any: ".mysql_error());

    //get the first (and only) row from the result 
    $row = mysql_fetch_array($result, MYSQL_ASSOC); 
    

$username=$row['username']; 
    $password=$row['password'];
    $first_name=$row['first_name'];
$test1=$row['test1']; 
    

html part:
<p>test1:</td><td><input type="checkbox" name="test1" value="<?php if($test1 == "on"){" checked=\"checked\"";}?>

Then we have the results page:

$first_name=$_POST['first_name'];
	$last_name=$_POST['last_name']; 
	if (isset($_POST['test1']) == "on")
    $test1 = "true";  
else
    $test1 = "null";

 $query = "UPDATE suppliers SET first_name='$first_name', etc etc.


Hugs and kisses to all of you if you can help with this!

#11 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 12:38 PM

You STILL hav not closed the checkbox tag!!!!!!!!!!!

<input type="checkbox" name="test1" value="<?php if($test1 == "on"){" checked="checked\"";}?> />
follow me on twitter @PHPsycho

#12 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 21 August 2006 - 12:41 PM

Ah sorry about that didn't post that, but have done it and no its still not working. Is it  something to do with?:

if (isset($_POST['test1']) == "on")
    $test1 = "true";  
else
    $test1 = "null";



#13 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 12:52 PM

run your script - have a look at the html source and see if it does echo checked.....

in fact run your script and post the html source on here...
follow me on twitter @PHPsycho

#14 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 21 August 2006 - 01:07 PM

Ok so the php code looks like so:

<p>test1:</td><td><input type="checkbox" name="test1" value="<?php if($test1 == "on"){"checked=\"checked\"";}?>/>
</p>

and this is what we get from the html source:

<p>test1:</td><td><input type="checkbox" name="test1" value="/>
</p>

Doesn't say 'checked' thats for sure...

#15 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 01:23 PM

didn't see it before....

value is an attribute that is not going to chnage so each check box should have a value set. What you want is.....

<input type="checkbox" name="test1" value="1"<?php if($test1 == "on"){echo "checked="checked\"";}?> />

<input type="checkbox" name="test2" value="2"<?php if($test2 == "on"){echo " checked=\"checked\"";}?> />

follow me on twitter @PHPsycho

#16 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 August 2006 - 01:44 PM

I can't understand why you're even using 'on'.  You're converting it for insert into the database anyway, so you may as well just use 'true' as the value

You'd be better off with this surely:

[code=php:0]
<input type="checkbox" name="test1" value="true" <?php if($test1 == "true"){echo "checked=\"checked\"";}?> />
[/code]

And then change your code on the results page to:

$first_name=$_POST['first_name'];
$last_name=$_POST['last_name']; 

if (isset($_POST['test1']) != "true"){
    $test1 = "null";
}

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#17 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 21 August 2006 - 02:59 PM

And we are there! Great thanks everybody!

#18 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 21 August 2006 - 03:03 PM

And we are there! Great thanks everybody!


Glad to have been of some help.

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users