0

I have the following code, however the problem is that if I use a normal submit button everything works fine, but if I want to submit the checkbox value with jquery onchange it doesn't work (i.e. nothing is going into the db)... I tried even going the non jquery route with an onChange=this.form.submit() appended to construction. Anyone know whats going on/how to fix this? To me it seem like the form is submitting without processing the data.

Also I'm using: https://github.com/ghinda/css-toggle-switch for the checkbox

JQUERY:

$('#construction').change(function() {
  this.form.submit();
});

PHP:

<?php
$config = mysql_query("SELECT construction FROM config") or die(mysql_error());
$row_config = mysql_fetch_assoc($config);

if ($_POST) {
    // 0 = off
    // 1 = on
    $constr = $_POST['construction'];
    if ($constr == 'on') { $c = 0; } else { $c = 1; }
    mysql_query("UPDATE config SET construction = '".$c."'") or die(mysql_error());
    redirect('index.php');
}
?>

HTML:

<div style="margin-top:30px;">
        <form action="index.php" method="post" id="doSite">
            <fieldset class="toggle">
                <input id="construction" name="construction" type="checkbox"<?php if($row_config['construction'] == '0') { echo ' checked'; } ?>>
                <label for="construction">Site construction:</label>
                <span class="toggle-button"></span>
            </fieldset>
            <input name="Submit" type="submit" value="Shutdown site" class="button">
        </form>
    </div>

3 Answers 3

2

Try this one,

$('#construction').change(function() {
  $(this).closest("form").submit();
});

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

it almost seems as though its submitting before processing the checkbox. any idea why? the data isnt going into the db
have you checked the posted data with <?php print_r($_POST) ?> ?
1

Change your jquery to:

$('#construction').change(function() {
  $("#doSite").submit();
});

doSite is your form id, so you may use it directly to submit the form when the checkbox is checked or unchecked.

I updated your code:
- use isset to check if a variable is set or not - checkboxes that are not checked upon form submission are not included in the $_POST data: it means you just have to check if $_POST['construction'] is set or not (its value doesn't matter)
- since you are using double quotes in mysql_query, there is no need to concatenate. And, if construction field is of INT type, you may remove the single quotes around $c

<?php
$config = mysql_query("SELECT construction FROM config") or die(mysql_error());
$row_config = mysql_fetch_assoc($config);

if(isset(($_POST))
{
    // 0 = off
    // 1 = on
    if(isset($_POST['construction']))
        $c = 1;
    else
        $c = 0;
    mysql_query("UPDATE config SET construction = '$c'") or die(mysql_error());
    redirect('index.php');
}
?>

Note: All mysql_* functions are going to be deprecated and removed some day. If possible, you should use MySQLi or PDO instead.
More on that:
http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated
http://php.net/manual/en/mysqlinfo.api.choosing.php

1 Comment

thank you. i updated the code as you suggested however I am still having the problem that it doesnt the data isnt going into the database. it almost seems as though its submitting before processing the checkbox. any idea why?
0

Why dont u use:

$("#checkid").click(function(){

})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.