如果jquery成功,如何运行php sql代码

时间:2016-04-12 21:35:01

标签: php jquery

所以我有一个问题,

如果我有这样的php文件:

<?php
        print
        "<div class='col-lg-6 col-md-6 col-sm-6 col-xs-12 col-md-pull-6 col-sm-pull-6'>".
                        "<div class='form-group'>".
                            "<div class='col-md-12'><strong>Forename:</strong></div>".
                            "<div class='col-md-12'><input type='text' class='form-control' id='forename'></div>".
                        "</div>".
                        "<div class='form-group'>".
                            "<div class='col-md-12'><strong>Surname:</strong></div>".
                            "<div class='col-md-12'><input type='text' class='form-control' id='surname'></div>".
                        "</div>".
                            "<div class='form-group'>".
                                "<div class='col-xs-12 col-md-6'>".
                                    "<button id='button' class='btn btn-success btn_add'>Add</button>".
                                "</div>".
                            "</div>".
                        "</div>".
        "</div>";

        }

?>

<script type="text/javascript">     
    $(document).on("click", 'button.btn_add', function(){
        var forename = document.getElementById('forename').value;
        var surname = document.getElementById('surname').value;
        if((forename == null || forename == "") && (surname == null || surname == "")){
            alert("Please fill in all fields");
        }
        else{

            // DO SQL UPDATE VIA PHP

        }
    });

</script>

如何在jquery中的else case上进行sql更新?理想情况下,我想验证然后执行sql语句

4 个答案:

答案 0 :(得分:1)

查看$.ajax

它可以让你做你想要的事。

.. code
else {
    $.ajax({
        url: some_url_to_execute_query.php,
        method: 'post',
        dataType: 'json',
        data: 'forename=' + forename + '&surname=' + surname
    })
     .done(function(response) {
        if (response.success) {
            // notify user query worked
        }
        else {
            // query didn't work; do something
        }
    }
}

PHP(some_url_to_execute_query.php)

<?php
$response = [];

// execute your MySQL query here
// make sure to connect to database, etc.
// access $_POST array values using $_POST['forename'] && $_POST['surname']
// then something like so (pseudo):

if ($query) // $query is pseudo variable holding (bool) response from query to determine whether is passed/failed
    $response['success'] = true;
else
    $response['success'] = false;

echo json_encode($response);

答案 1 :(得分:0)

你只能从服务器端进行SQL查询,你需要一些API来实现它。

您可以使用php打印JavaScript,但无法使用JavaScript从客户端执行php。

答案 2 :(得分:0)

你需要使用jquery进行ajax调用才能运行sql查询。

http://api.jquery.com/jquery.ajax/

所以你需要一个单独的php脚本来运行sql查询,然后你将使用ajax从你的表单中发布值。

答案 3 :(得分:0)

像这样:

请注意type =&#34;按钮&#34;

Imports Microsoft.Office.Interop

Dim Access_Object As New Access.Application
Access_Object = CreateObject("Access.Application")
Access_Object.Application.Visible = True