weixin_33738555 2014-09-09 15:52 采纳率: 0%
浏览 16

使用AJAX发布到PHP链接

I have been trying to use a wamp server with my html site. I want to add a row to a table but nothing seems to work. I am using ajax to post the data to the php file and then using sql commands to update the table.

HTML Code

<button type="submit" class="btn btn-lg btn-primary" onClick="PostJob();"> Submit </button>

Javascript code in HTML file:

    <script language="javascript">
    function PostJob() {
        $.ajax({
        type: "POST",
        url: "http://localhost/jobjobDatabase/update_job.php",
        data: {ProjectName: $("#ProjectName").val()}
        });
    }
    </script>

PHP Code:

<?php


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


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_POST['ProjectName'])) {
    $ProjectName = $_POST['ProjectName']; //Project Name
    echo $ProjectName;




    // mysql inserting a new row
    $result = mysql_query("INSERT INTO postedjobs(ProjectName) 
                       VALUES('$ProjectName')");


    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "games successfully created.";

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

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

    // echoing JSON response
    echo json_encode($response);
}
?>

Any ideas on what I am doing wrong or I should try? I have been googleing and reading/trying everything and nothing seems to work. Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器