将表单从jquery提交到php

将表单从jquery提交到php,php,jquery,mysqli,Php,Jquery,Mysqli,我快疯了!我正在尝试将一个表单从jquery提交到php,并在数据库中插入一条记录。我没有收到任何错误,但没有提交任何记录。当我转到php页面并在URL中放入变量时,一切都很好。但是,当我通过jquery页面提交变量时,它不起作用。有人能帮我吗 HTML: tellus.php: <?php require_once ('../constants_test.php'); $name = $_GET['name']; $db = new mysqli(DB_HOST,

我快疯了!我正在尝试将一个表单从jquery提交到php,并在数据库中插入一条记录。我没有收到任何错误,但没有提交任何记录。当我转到php页面并在URL中放入变量时,一切都很好。但是,当我通过jquery页面提交变量时,它不起作用。有人能帮我吗

HTML:

tellus.php:

<?php
require_once ('../constants_test.php'); 

    $name = $_GET['name'];

    $db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
                if (mysqli_connect_errno()) {
                        printf("Connect failed: %s", mysqli_connect_error());
                        exit;
                }

    $q = "Insert into tellus(`Name`) values ('" . $name . "')";
    if ($db->query($q)) {
        echo "console.log('you got it')";
    };
    $db->close();

?>
使用:

还要确保表单元素上有名称:

<input type="text" name="tellAge" id="tellAge" />

此主题在这里非常常见,因此请尝试搜索其他帖子-例如,也可以使用。

我建议您应该使用POST执行操作,包括插入/删除/更新数据库中的数据。它保证了数据的安全。
I suggest you should use POST for actions which include insert/delete/update of data in database.  It keeps the data safe.

If you want to still use GET and test out something.  I suggest do not use jquery in the middle to handle the get.  Submit button on click submits the form anyway, so the jquery $.get is unnecessary.

To your form element add 
<form name='' action='tellus.php'>...</form>

This should fix the problem.
如果你想继续使用GET并测试一些东西。我建议不要在中间使用jQuery来处理GET。单击Submit按钮仍会提交表单,因此jquery$.get是不必要的。 在表单元素中添加 ... 这应该可以解决问题。
这个怎么样

jquery

$("#submit").click(function() {
    formData = {
        name: "abc"
    }
    $.ajax({
        type: 'GET',
        contentType: 'application/json',
        url: "http://yourpage/tellus.php",
        dataType: "json",
        data: formData,
        success: function(data) {
            console.log(data.message);
        },
        error: function(data) {
            console.log(data.message);
        }
    });
});
php


谢谢所有试图帮助我的人!我最后不得不这样做:

$.post("./tellus.php", {tellName:name}, function(data){
alert(data)
}); 

ajax调用一定有一些不起作用的东西。

ajax调用不起作用,因为您正在使用
$。serialize
序列化表单对象,它将返回

{key:'tellName', value='name'} // <input name="tellName" value="name" />
并在提交表单时调用它。它将构造表单,使其具有
键:值

<form id="register">
    <input type="text name="username" />
    <input type="text" name="email" />
    <input type="submit value="Register!" />
</form>

干杯

至少尝试阻止提交单击的默认行为:
$('.submit')。单击(函数(e){e.preventDefault();$.get(…);})不要对返回的数据使用
eval()
。当你正在修改数据库中的值时,你应该强烈考虑使用POST而不是获取。您是否对查询本身进行过任何调试,以查看是否出现错误(因为您根本没有处理错误案例)?调试它?;-)例如,在Google Chrome中,您可以查看正在提交的XHR(Ajax)(按
F12
并查看控制台-在设置中启用“Log XMLHttpRequests”),您可以在
tellus.php
文件中输出各种内容,以查看变量是否如预期的那样。@Mike Brant:我使用GET只是为了弄清楚发生了什么。一开始我使用的是POST。无论是get还是POST,您都没有正确地序列化表单,PHP正在捕获意外情况。请参阅我的答案以获得解决方案。我看到它正在提交,但tellus.php页面上出现了一些问题。我如何对该页面进行故障排除?或者,我如何确定该页面抛出了什么错误消息?我的朋友发出了什么错误消息?你能给我看看吗?@LauraNMS-直接在浏览器中调用该页面并添加URL参数,
http://yourpage/tellus.php?name=abc
@JohnMalli:当我这样做时,它会起作用,但当我提交表格时就不会起作用。@Dato:也谢谢你!
  <?php

// array for JSON response
$response = array();

// check for required fields
if (isset($_GET['name'])) {

    $name = $_GET['name'];

    // include db connect class
    require_once ('../constants_test.php'); 

    // connecting to db
    $db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
                    if (mysqli_connect_errno()) {
                            printf("Connect failed: %s", mysqli_connect_error());
                            exit;
                    }

    // mysqli inserting a new row
    $q = "INSERT INTO tellus('Name') values ('" . $name . "')";

    // check if row inserted or not
    if ($db->query($q)) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "you got it";

        // echoing JSON response
        echo json_encode($response);
        $db->close();
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
        $db->close();
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>
$.post("./tellus.php", {tellName:name}, function(data){
alert(data)
}); 
{key:'tellName', value='name'} // <input name="tellName" value="name" />
$.fn.serializeObject = function(){
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
                o[this.name].push(this.value || '');
            } else  {
                o[this.name] = this.value || '';
            }
        });
    return o;
}
<form id="register">
    <input type="text name="username" />
    <input type="text" name="email" />
    <input type="submit value="Register!" />
</form>
$('#register').on('submit', function(){
    var data = $(this).serializeObject()
    // data will return {username: value, email:value}
    $.post('process.php', data, function(response){
        try{
            var r = JSON.parse(response) // Check if the response is an object
            console.log(JSON.stringify(r))
        }
        catch(e){
            console.log(response) // The response is a string
        }

    })
})