使用php ajax提交表单

时间:2017-03-17 16:42:55

标签: javascript php jquery ajax html5

嗨,大家好我是网络开发的新手,我在尝试学习某些事情时遇到了麻烦这是我的html文件

<!DOCTYPE html>
<head>
    <title>test</title>
    <style type="text/css">
    </style>
    <link src="jquery-3.1.1.slim.min.js"></script>
    <link src="myjs1.js"></script>
    <script type="text/javascript">
        $("submit").click(function(){
            $. Ajax ({
              type: 'post',
              url: db.php ,
              data: les données ,
              success: alert('L\'action est envoyée') ,
              dataType: dataType
            });
            function(data, status){
                alert("Data");
            };
        });
    </script>
</head>
    <body>
        <div style="position: relative;" id="form-content">
        <form method="post" id="form">
            id     :<input type="text" name="id" placeholder="id"><br><br>
            nom    :<input type="text" name="nom" placeholder="nom"><br><br>
            prenom :<input type="text" name="prenom" placeholder="prenom"><br><br>
            age    :<input type="text" name="age" placeholder="age"><br><br>
            <input type="submit" name="add" value="Ajouter" id="click">
        </form>
        </div>
        <span id="span1" style="position: relative;">
            <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d83998.94722685362!2d2.2775168640288337!3d48.85883773913696!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x47e66e1f06e2b70f%3A0x40b82c3688c9460!2sParis!5e0!3m2!1sen!2sfr!4v1489763283156" width="600" height="450" frameborder="0" style="border:0" allowfullscreen></iframe>
        </span>
    <body>
</html>

这是db.php文件

<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "test";

// Create connection
$con =  mysqli_connect("$servername", "$username", "$password", "$dbname");

// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}

// Local variables
$id = (isset($_POST['id']) ? $_POST['id'] : '');
$nom = (isset($_POST['nom']) ? $_POST['nom'] : '');
$prenom = (isset($_POST['prenom']) ? $_POST['prenom'] : '');
$age = (isset($_POST['age']) ? $_POST['age'] : '');

// Sql reqest
$sql= "INSERT INTO prsn (id,nom,prenom,age) VALUES ($id,'$nom','$prenom',$age)";

$query=mysqli_query($con,$sql);

header("Location:index.php");

?>

我想要的是如何使用ajax,以便在我点击按钮时无法加载网页

0 个答案:

没有答案
相关问题