Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
让jquery和php与表单协同工作?_Php_Jquery_Ajax_Forms_Form Submit - Fatal编程技术网

让jquery和php与表单协同工作?

让jquery和php与表单协同工作?,php,jquery,ajax,forms,form-submit,Php,Jquery,Ajax,Forms,Form Submit,所以我有一个简单的问题,但我找不到答案 我有一个表单,在该表单中,用户在文本字段中键入内容,然后单击值为“Add”的submit按钮。右侧将有一个列表,每次用户单击“添加”时,右侧列表中将添加一个淡入动画的元素 我使用php来创建它,这样每当用户单击add时,它都会查询数据库并找到用户要查找的内容。如果它不在数据库中,则将其插入数据库 我使用javascript/jquery在用户单击“添加”时进行淡入动画 我知道如何单独执行这些操作,但是当我单击Add按钮(submit按钮)时,整个页面都会刷

所以我有一个简单的问题,但我找不到答案

我有一个表单,在该表单中,用户在文本字段中键入内容,然后单击值为“Add”的submit按钮。右侧将有一个列表,每次用户单击“添加”时,右侧列表中将添加一个淡入动画的元素

我使用php来创建它,这样每当用户单击add时,它都会查询数据库并找到用户要查找的内容。如果它不在数据库中,则将其插入数据库

我使用javascript/jquery在用户单击“添加”时进行淡入动画

我知道如何单独执行这些操作,但是当我单击Add按钮(submit按钮)时,整个页面都会刷新,php工作正常,但没有动画


我尝试在jquery上使用preventDefault(),动画效果很好,但是php代码没有注册?我该如何使php和javascript不会相互中断?这和ajax有什么关系吗?谢谢

您需要使用jquery Ajax函数。这些都是专门设计的,这样您就可以在不刷新页面的情况下调用php脚本


有关Ajax post函数的官方文档,以及如何使用它们。

这里是我提出的一个示例。希望这能对你有所帮助

index.php的内容 这是您的表单所在的位置以及添加的项目将显示的位置

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>Page Title</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
        <script type="text/javascript">
        <!--
            jQuery(function($) {
                // Declare DOM elements for quick access
                var itemsList = $('#items-list'),
                    searchInput = $('#search-input');

                // click event handler for the 'Add' button
                $('#add-btn').click(function(e) {
                    // Prevent the form from being sent
                    e.preventDefault();

                    var searchValue = searchInput.val();

                    // Send the AJAX request with the search parameter
                    $.post('search.php', {
                            search: searchValue
                        },
                        function(data, textStatus) {
                            // data is returned as a json object
                            if (data.found) {
                                // Create a new hidden element into the list
                                // and make it fade in
                                $('<p class="item">'+searchValue+'</p>').hide()
                                    .appendTo(itemsList)
                                    .fadeIn();
                            }
                        }, 'json'
                    });
                });
            });
        //-->
        </script>
    </head>
    <body>
        <form action="index.php" method="post" id="search-form">
            <div>
                <input type="text" name="search" id="search-input"> 
                <input type="submit" id="add-btn" value="Add">

                <div id="items-list"></div>
            </div>
        </form>
    </body>
</html>

页面标题
search.php的内容
FALSE);
//检查是否提供了搜索参数
$search=filter\u input(input\u POST,'search',filter\u SANITIZE\u STRING);
如果(!空($search)){
//注意:我们假设连接到MySQL数据库
//已经声明了以下常量
$mysqli=新的mysqli(DB\u主机名、DB\u用户名、DB\u密码、DB\u名称);
//确保连接成功
如果(!$mysqli->连接错误){
$query=“从搜索_表中选择id,关键字,其中关键字=?”;
//检查搜索关键字是否已存在
$stmt=$mysqli->prepare($query);
$stmt->bind_参数('s',$search);
$stmt->execute();
//如果找不到,请创建新条目
如果(0=$stmt->num_rows()){
$query=“插入搜索表格(关键字)值(?);
$stmt=$mysqli->prepare($query);
$stmt->bind_参数('s',$search);
$response['found']=$stmt->execute();
}
否则{
$response['found']=TRUE;
}
}
}
echo json_编码($response);
}
这是没有测试,所以让我知道如果你遇到任何问题


干杯,

欢迎来到stackoverflow,请您尝试更好地解释您的问题,或者发布一些代码示例?我很难理解你的实际问题。嘿,亚历克斯,我把我的问题澄清了一点。我问了一个一般性的问题,点击表单上的提交按钮会刷新页面,并阻止我的javascript动画实现。
<?php

// AJAX Request?
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    // Prepare the reponse
    $response = array('found' => FALSE);

    // Check that the search parameter was provided
    $search = filter_input(INPUT_POST, 'search', FILTER_SANITIZE_STRING);

    if (!empty($search)) {
        // Note: We'll assume a connection to a MySQL database
        // with the following constant already declared
        $mysqli = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_NAME);

        // Make sure that the connection was successful
        if (!$mysqli->connect_error) {
            $query = "SELECT id, keyword FROM search_table WHERE keyword = ?";

            // Check if the search keyword already exists
            $stmt = $mysqli->prepare($query);
            $stmt->bind_param('s', $search);
            $stmt->execute();

            // Create a new entry if not found
            if (0 == $stmt->num_rows()) {
                $query = "INSERT INTO search_table(keyword) VALUES(?)";

                $stmt = $mysqli->prepare($query);
                $stmt->bind_param('s', $search);

                $response['found'] = $stmt->execute();
            }
            else {
                $response['found'] = TRUE;
            }
        }
    }

    echo json_encode($response);
}