简体   繁体   中英

Can't send data with ajax to my php

Can't seem to send data from my html form to a php file which inserts the data into a mysql database. One of the forms as an example:

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

The ajax script:

<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>

And the php file:

<?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);

?>

The php file inserts a null row into the databse. I tried for hours now and i got so far, that at least one variable from the html form got passed to the php file.

Hope someone can help me

Change $_post to $_POST

For Example

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

向量$ _POST必须为大写。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM