Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/18181-checkbox-echo-issue/
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
Link to comment
https://forums.phpfreaks.com/topic/18181-checkbox-echo-issue/#findComment-77971
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..
Link to comment
https://forums.phpfreaks.com/topic/18181-checkbox-echo-issue/#findComment-77973
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
Link to comment
https://forums.phpfreaks.com/topic/18181-checkbox-echo-issue/#findComment-77979
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!
Link to comment
https://forums.phpfreaks.com/topic/18181-checkbox-echo-issue/#findComment-78001
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...
Link to comment
https://forums.phpfreaks.com/topic/18181-checkbox-echo-issue/#findComment-78032
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\"";}?> />
Link to comment
https://forums.phpfreaks.com/topic/18181-checkbox-echo-issue/#findComment-78041
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
Link to comment
https://forums.phpfreaks.com/topic/18181-checkbox-echo-issue/#findComment-78059
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.