0

私は次のコードを持っていますが、問題は、通常の送信ボタンを使用するとすべてが正常に機能することですが、jquery onchange でチェックボックスの値を送信したい場合は機能しません (つまり、データベースには何も入っていません)。onChange=this.form.submit()追加の構造を使用して、jquery以外のルートに行ってみました。何が起こっているのか/これを修正する方法を知っている人はいますか? 私には、フォームがデータを処理せずに送信されているようです。

また、チェックボックスにhttps://github.com/ghinda/css-toggle-switchを使用しています

Jクエリ:

$('#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>
4

3 に答える 3

2

これを試して、

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

デモ

于 2012-07-30T01:03:36.987 に答える
1

jquery を次のように変更します。

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

doSiteはフォーム ID であるため、チェックボックスがオンまたはオフのときにフォームを送信するために直接使用できます。

私はあなたのコードを更新しました:
- issetを使用して、変数が設定されているかどうかを確認します - フォーム送信時にチェックされていないチェックボックスは $_POST データに含まれていません: $_POST['construction']設定されているかどうか (値は関係ありません)
- mysql_query で二重引用符を使用しているため、連結する必要はありません。また、constructionフィールドが INT タイプの場合、$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');
}
?>

注: すべての mysql_* 関数は非推奨になり、いつか削除される予定です。可能であれば、代わりにMySQLiまたはPDOを使用してください。
詳細:
http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated
http://php.net/manual/en/mysqlinfo.api.choosing.php

于 2012-07-30T01:06:09.547 に答える
0

使用しない理由:

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

}))

于 2012-08-20T11:31:59.063 に答える