/    Sign up×
Community /Pin to ProfileBookmark

Can not post checkbox value

Hi

I wonder if someone can help, I have an edit form where I’m getting data from database and displaying and amending. The checkboxes work fine when I’m displaying the data but I can NOT seem to post checkbox value back to database. The form doesn’t seem to post the value it submits fine.

[code=php]$query2 = “SELECT query works fine
$result2 = mysql_query ( $query2 ) or die ( mysql_error () );
while ( $row = mysql_fetch_array ( $result2 ) ) {
extract ( $row );

<input type=”checkbox” name='<?php echo $row[‘region’]; ?>’ value='<?php echo $row[‘uid’]; ?>’ <?php if(! empty($row[‘uid’])) echo ‘checked’; ?> />

<?php }?>[/code]

Many thanks
Zed

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@rootMar 20.2015 — You need to supply the code you are using, you have not provided enough code, what you have provided is just an output routine.
Copy linkTweet thisAlerts:
@zed420authorMar 20.2015 — You need to supply the code you are using, you have not provided enough code, what you have provided is just an output routine.[/QUOTE]

Thanks for your reply, can you tell by looking at checkbox input, I thing the dynamic Value is causing the problem unless I'm totally off the tangent.
Copy linkTweet thisAlerts:
@rootMar 20.2015 — Hi

I wonder if someone can help, I have an edit form where I'm getting data from database and displaying and amending. The checkboxes work fine when I'm displaying the data but I can NOT seem to post checkbox value back to database. The form doesn't seem to post the value it submits fine.
[code=php]$query2 = "SELECT query works fine
$result2 = mysql_query ( $query2 ) or die ( mysql_error () );
while ( $row = mysql_fetch_array ( $result2 ) ) {
extract ( $row );

<input type="checkbox" name='<?php echo $row['region']; ?>' value='<?php echo $row['uid']; ?>' <?php if(! empty($row['uid'])) echo 'checked'; ?> />

<?php }?>[/code]


Many thanks

Zed[/QUOTE]


Here is a rough idea...

[code=php]$query2 = "SELECT query works fine
$result2 = mysqli_query ( $con, $query2 );
if( $con->connect_errno )
die("[Database connection error] ". $con->connect_error);

while ( $row = $result2->fetch_array() ) {

$checked = !empty($row['uid']) ? "checked" : "";

echo <<<HEREDOC
<input type="checkbox" name='{$row['region']}' value='{$row['uid']}' $checked />
HEREDOC;
}
[/code]


$con in the example is the connection handler variable, depending on how you make the connection object depends on how to proceed...

If you [code=php] $con = new mysqli("localhost","user","pass","database");[/code] then you have the OO type of functions to use like $result = $con->query( "Select..." );

With the [code=php] $con = mysqli_connect("localhost","user","password","database");[/code] method of creating an handler, your using procedural type functions like $result = mysqli_query( $con, "Select...");

And... STOP! using mysql_query, it is now depreciated and in many web servers no longer works as PHP have removed it, use mysql[b]i[/b]_* functions and also you need to use a variable as a link handler.

That will output the check boxes based on the database results, as for the update to the server, you need to post the code that submits the form to the server for processing.
Copy linkTweet thisAlerts:
@NogDogMar 20.2015 — Not sure if this is an issue (did not read through the whole thread in fine detail), but a checkbox key/value pair only gets passed to the form-handler if the checkbox is checked. Therefore, your code will need to see if it even exists in the POST (or GET) data before trying to use it. (If it's not set, then assume it was unchecked.)
×

Success!

Help @zed420 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...