只需单击一下即可调用PHP和对话框

时间:2015-05-20 06:47:36

标签: javascript php jquery html

我有一个表单,我想在对话框中向用户显示他们的输入数据,并且只使用一次点击就可以使用PHP保存在SQL中。问题是我一次只能调用一个(对话框或PHP)。由于对话框需要anchor标记

<a href="#" value="Save" onclick="submit()">Save</a>
<script>
    document.forms["myform"].reset();
    $(function(){
        $("#dialog").dialog({
            autoOpen:false,
            modal:true,
            width:400,
            height:300,
            buttons:{
                "OK": function(){
                        $(this).dialog("close");
                        alert("Saved");

                    },
                "Cancel":function(){
                        $(this).dialog("close");
                    }
            }
        });

    });
    function submit(){
        $("#location").html($("#pac-input").val());
        $("#type").html($("input:radio[name='sex']:checked").val());
        $("#open").html($("#open").val());
        $("#close").html($("#close").val());
        $("#review").html($("#review").val());
        $("#rating").html($("input:radio[name='rating']:checked").val());
        $("#dialog").dialog("open");
        }
    </script>

和PHP需要提交按钮来存储值

<input type="submit" name="submit">
<?php
            $address= $_POST['address'];
            $type = $_POST['sex'];
            $open = $_POST['open'];
            $close = $_POST['close'];
            $review = $_POST['review'];
            $rating = $_POST['rating'];
            $dbname = "addtoilet";
            // Create connection
            $conn = mysqli_connect("localhost:3306", "root","",$dbname);

            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } else{
            echo "Connected successfully";
            }
            $sql = "INSERT INTO add (address, type, open, close)
                VALUES ('$address', '$type', '$open', '$close')";
            if (mysqli_query($conn, $sql)) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($conn);
            }

            mysqli_close($conn);
        }
        ?>

2 个答案:

答案 0 :(得分:1)

在对话框的ok函数中写入submit函数。

<a href="#" value="Save" onclick="submit()">Save</a>
<script>
document.forms["myform"].reset();
$(function(){
    $("#dialog").dialog({
        autoOpen:false,
        modal:true,
        width:400,
        height:300,
        buttons:{
            "OK": function(){
                    $("#your_form_id").submit();// also you can call ajax here if you don't want to reload the page
                    $(this).dialog("close");
                    alert("Saved");

                },
            "Cancel":function(){
                    $(this).dialog("close");
                }
        }
    });

});
function submit(){
    $("#location").html($("#pac-input").val());
    $("#type").html($("input:radio[name='sex']:checked").val());
    $("#open").html($("#open").val());
    $("#close").html($("#close").val());
    $("#review").html($("#review").val());
    $("#rating").html($("input:radio[name='rating']:checked").val());
    $("#dialog").dialog("open");
    }
</script>

答案 1 :(得分:0)

您必须使用Jquery ajax。这将在同一次点击后将数据发布到php。您也可以使用速记方法POST。
将要以json格式发布的数据保存在变量说数据中,并将其传递给post方法。

功能提交(){
var location = $(“#pac-input”)。val();
var data =“{'location':'”+ location +“'}”;

var jqxhr = $ .post(“example.php”,data,function(){
 console.log(“成功”);
})
  .done(function(){
$( “#对话”)对话框( “开放”)。

})
  .fail(function(){
 console.log(“错误”);
  })
  .always(function(){
 console.log(“已完成”);
});

}
您可以根据post方法的成功打开对话框