Jump to content

Archived

This topic is now archived and is closed to further replies.

genista

Checkbox echo issue

Recommended Posts

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:

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

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

Share this post


Link to post
Share on other sites
For a start there's no closing tag on the <Input> tag...

Try this:

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

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:

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

Regards
Rich

Share this post


Link to post
Share on other sites
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:

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

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..

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[quote author=ToonMariner link=topic=105042.msg419336#msg419336 date=1156157482]
swap echo " CHECKED" for echo " checked=\"checked\"";
[/quote]

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

Share this post


Link to post
Share on other sites
Yeah I know but I just like standards - if the doctype is strict then I think just CHECKED will fail!

Share this post


Link to post
Share on other sites
[quote]
Yeah I know but I just like standards
[/quote]

Someone after my own heart  8)

Share this post


Link to post
Share on other sites
Ok I have done that but still nothing, here are the two pages:

updatedetails.php
[code=php:0]
$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\"";}?>
[/code]

Then we have the results page:

[code=php:0]
$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.

[/code]

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

Share this post


Link to post
Share on other sites
You STILL hav not closed the checkbox tag!!!!!!!!!!!

<input type="checkbox" name="test1" value="<?php if($test1 == "on"){" checked="checked\"";}?> [color=red]/>[/color]

Share this post


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

[code=php:0]
if (isset($_POST['test1']) == "on")
    $test1 = "true"; 
else
    $test1 = "null";
[/code]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Ok so the php code looks like so:

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

and this is what we get from the html source:

[code=php:0]
<p>test1:</td><td><input type="checkbox" name="test1" value="/>
</p>
[/code]

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

Share this post


Link to post
Share on other sites
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\"";}?> />

Share this post


Link to post
Share on other sites
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 '[color=red]true[/color]' as the value

You'd be better off with this surely:

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

And then change your code on the results page to:

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

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

Regards
Rich

Share this post


Link to post
Share on other sites
[quote]
And we are there! Great thanks everybody!
[/quote]

Glad to have been of some help.

Regards
Rich

Share this post


Link to post
Share on other sites

×

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.