无法使用ajax将数据发送到我的php

时间:2014-10-21 18:14:35

标签: javascript php mysql ajax

似乎无法将我的html表单中的数据发送到将数据插入mysql数据库的php文件中。 其中一种形式为例:

<div class="panel-body">
    <div class="input-group">
        <input type="text" id="vorID" name="form_vorname" class="form-control" placeholder="Vorname">
    </div>
</div>

ajax脚本:

<script>
    $(function(){
        $('#submit_button').on('click', function(e){        
            e.preventDefault(); // preventing default click action
            $.ajax({
                url: 'insert_ma.php',
                type: 'post',
                data: { 
                    vorname:$   ('#vorID').val(),
                    nachname:$  ("#nachid").val(), 
                    plz:$       ("#plzid").val(), 
                    ort:$       ("#ortid").val(), 
                    tel:$       ("#telid").val(), 
                    email:$     ("#emailid").val()
                },
                success: function(output){
                    alert('Erfolgreich');

                }, error: function(output){
                    alert('ajax failed');
                },
            })
        })

    })
</script>

和php文件:

<?php

//Connection Details
$username = 'root';
$password = '';
$hostname = 'localhost';
$databasename = 'plzdb';


$vorname     = ($_post['vorname']);
$nachname    = ($_post['nachname']);
$plz         = ($_post['plz']);
$ort         = ($_post['ort']);
$tel         = ($_post['tel']);
$email       = ($_post['email']);


//Connection-string
$con = mysqli_connect($hostname,$username,$password,$databasename);
//$mysqli = new mysqli($hostname,$username,$password,$databasename);

//SQL Query

$sql = "INSERT into plz_person (per_vorname, per_nachname, 
            per_plz, per_Ort, per_tel, per_email, per_bild) 
        VALUES 
        ('$vorname','$nachname','$plz','$ort','$tel','$email','')";



if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

//Close Connection
mysqli_close($con);

?>

php文件在数据库中插入一个空行。我现在试了好几个小时,到目前为止,至少有一个来自html表单的变量被传递给了php文件。

希望有人可以帮助我

2 个答案:

答案 0 :(得分:0)

$_post更改为$_POST

例如

$vorname     = $_POST['vorname'];
$nachname    = $_POST['nachname'];
$plz         = $_POST['plz'];
$ort         = $_POST['ort'];
$tel         = $_POST['tel'];
$email       = $_POST['email'];

答案 1 :(得分:0)

向量$ _POST必须为大写。