简体   繁体   English

PHP未将隐藏输入字段的值输入数据库

[英]PHP not entering value of hidden input field into database

I've got a site that has a custom select menu which uses jQuery to pass the selected value into the field. 我有一个具有自定义选择菜单的网站,该菜单使用jQuery将选定的值传递给该字段。 PHP then gets the value of the hidden input field and, should, insert the value into a database. 然后,PHP获取隐藏输入字段的值,并且应该将值插入数据库中。 However, this isn't happening and I'm not sure why. 但是,这没有发生,我不确定为什么。 Can anyone explain why this is happening? 谁能解释为什么会这样?

FORM 形成

<form id="scenario_builder" method="post">
    <fieldset for="center">
        <label>Center:</label>
        <div class="select" name="center_menu" id="center_menu">
            <div class="arrow"></div>
            <div class="option-menu">
                <?php
                    $query = "SELECT * FROM $centers";
                    $result = mysqli_query($connect, $query);
                    while($row = mysqli_fetch_assoc($result)){
                        $center_name = "{$row['center']}";
                        echo "<div class='option'>" .$center_name ."</div>";
                    }
                ?>
            </div>
        </div>
    </fieldset>
    <input type="submit" name="save" id="save" class="button" value="Save" />
</form>

FORM PROCESSING 表格处理

ob_start();
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $center = $_POST["center"];
    $query = "INSERT INTO `$scenarios`(`id`) VALUES('" .$center ."')";
    mysqli_query($connect, $query);
}
ob_clean();
echo json_encode(array("success" => 1));

JQUERY SELECT MENU JQUERY选择菜单

function select_menu(){
    var select = $(".select");
    var option_menu = $(".option-menu");
    var option = $(".option");
    select.on("click", function(){
        $(this).find(option_menu).toggle();
        select = $(this);
        $(this).find(option_menu).each(function(){
            $(".current").hide();
            if($(this).hasClass("current")){
                $(this).removeClass("current");
            }
            else{
                $(this).show().addClass("current");
            }
        })
    })

    option.on("click", function(){
        select.children("p").remove();
        var value = $(this).text();
        select.prepend("<p><input type='hidden' name='center' id='center' value='" +value +"' />" +value +"</p>");
        console.log(value);
    })

    $(document).on("click", function(ev){
        if($(ev.target).closest(".select").length === 0){
            $(".current").hide().removeClass("current");
        }
    })
}
select_menu();

JQUERY EVENT HANDLER JQUERY事件处理程序

$("input[id='save']").on("click", function(){
        $.post("..php/processing.php", {}, function(response){
            if(response.success == "1"){
                console.log("Data entered.");
            }
            else{
                console.log("Data not entered.");
            }
        }, "json");
    })

Your main issue is that you need to provide data to send using the data argument for $.post 您的主要问题是,您需要使用$.post的data参数提供要发送的数据

You can use serialize() to get all data for a form. 您可以使用serialize()获取表单的所有数据。 You also need to revent the browser default submit process when using ajax or the form will submit normally also 使用ajax时,您还需要避免浏览器默认的提交过程,否则表单也将正常提交

It is generally better to bind everyhting to the submit event of the form. 它通常是更好地结合everyhting到submit表单的事件。

$('#scenario_builder').submit(function(event){
    /* prevent default submit*/
     event.preventDefault();
     /* do ajax "this" is the form element*/
     $.post("..php/processing.php", $(this).serialize(), function(response){
            if(response.success == "1"){
                console.log("Data entered.");
            }
            else{
                console.log("Data not entered.");
            }
        }, "json");           
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM