duanli12176 2015-04-15 11:36
浏览 65
已采纳

信息没有传递给PHP脚本

I'm trying to create a form that when submitted sends the information over and refreshers the page, returning the id of the item submitted. I am able to get php to run and enter the data into the server but getting stuck with the AJAX, as all its doing is entering the text into the url bar.

AJAX Code

$(document).ready(function(){
$('#dataSubmut').on('dataSubmit',function(e){
    $.ajax({
        url:'files/insert.php',
        data:$(this).serialize(), // missing () on serialize()
        type:'POST',
        success:function(data){
            console.log(data);
            if(data != "Error") {
               $("#success").html(data).show().fadeOut(5000);
            }
            else {
               $("#error").html(data).show().fadeOut(5000);
            }
        },
        error:function(data){
            $("#error").show().fadeOut(5000);
        }
    });
    e.preventDefault();
});
});

HTML Code

<form name="dataSubmit" id="dataSubmit" action="">
Product Name: <input type="text" name="Product_Name" value=""><br>
Product Description: <input type="text" name="Product_Desc" value="">    <br>
Product Price: <input type="text" name="Product_Price" value=""><br>
Product Stock Amount: <input type="text" name="Product_Stock" value=""><br>
<input type="submit" >
<div id="data"></div>

PHP Code

<?php

$product_name = $_POST["Product_Name"];
$product_desc = $_POST["Product_Desc"];
$product_price = $_POST["Product_Price"];
$product_stock = $_POST["Product_Stock"];

$dsn = 'mysql:host=localhost;dbname=cms';
$user = 'root';
$password = '';
try {
$pdo = new PDO($dsn, $user, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}


$sql = "INSERT INTO Products (Product_Name, Product_Desc,      Product_Price, Product_Stock) 
        VALUES (:Product_Name, :Product_Desc, :Product_Price, :Product_Stock)";

$stmt = $pdo->prepare($sql);

$stmt->bindParam(':Product_Name', $_POST['Product_Name'], PDO::PARAM_STR);       
$stmt->bindParam(':Product_Desc', $_POST['Product_Desc'], PDO::PARAM_STR); 
$stmt->bindParam(':Product_Price', $_POST['Product_Price'], PDO::PARAM_STR); 
$stmt->bindParam(':Product_Stock', $_POST['Product_Stock'], PDO::PARAM_STR); 


$stmt->execute(); 
$newId = $pdo->lastInsertId();
echo "Data entered successfully with the id: ".$newId;

?>

  • 写回答

1条回答 默认 最新

  • dongqiabei7682 2015-04-15 11:39
    关注

    seems your id and event is wrong:

    $('#dataSubmut').on('dataSubmit'
    

    which should be :

    $('#dataSubmit').on('submit'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?