dtbonklcs575884485 2017-04-23 19:49
浏览 68
已采纳

Ajax调用php文件无法正常工作

I am trying to implement a simple form which will eventually connect to a database and make entries in it. In the tag,I am calling the php file which will connect me to the database in the back-end.

index.html

<html>
<head>
<script>
function submitForm(formId){

    //var formData= $.(formId).serialize();
    $.ajax({
        url:'new-user.php',
        type:'POST',
        data:{
            user_name=$("#user_name").val(),
            password=$("#password").val();
        }
        success:function(response){
            alert(response);
        }
    });
}
</script>
</head>

<body>
<form onsubmit="submitForm('#myForm');" id='myForm'>
User Name: <input type="text" name="user_name" id="user_name" />
Password: <input type="text" name="password" id="password" />
<input type="submit"/>
</form>
</body>

</html>

new-user.php

<?php include 'database.php';?>

<?php 
mysqli_query($connect,"create table login(User_name varchar(50) NOT NULL,Password varchar(50) NOT NULL)");
$user_name=$_POST['user_name'];
$password=$_POST['password'];
if(empty($user_name)){
    $name_error="name is required";
}

mysqli_query($connect,"Insert into login(User_name,Password) values('$user_name','$password')");
if(mysqli_affected_rows($connect)>0){
    echo "<p>Credentials added</p>";
    echo "<a href='index.html'>Go back</a>";
}else{
    echo "<p>Error</p>";
    echo mysqli_error($connect);
}
?>

database.php

<?php
$connect=mysqli_connect('localhost','root','','testdb');
if(mysqli_connect_errno($connect)){
    echo 'failed to connect';
}
?>

The above is not creating any table in the testdb database.Neither,it is generating any alert messages.The Url however changes after clicking the submit button as http://localhost/try2/?user_name=aayushi&password=ded but after that nothing happens. This is my first php code, so I don't really know what's the meaning of this exactly.

  • 写回答

2条回答 默认 最新

  • douxianglu4370 2017-04-23 20:00
    关注

    Okay, since no one seems to actually be reading your code, there's a couple of syntax errors that I missed until I threw it into PhpStorm

    Change your function to this:

    function submitForm(formId){
    
        $.ajax({
            url:'/new-user.php',
            type:'POST',
            data:{
                user_name: $("#user_name").val(),
                password: $("#password").val()
            }
        })
    
            .complete(function (response) {
                alert(response)
            })
    
       return false; // Prevents the form from submitting the standard way
    }
    

    EDIT: Change the form to this:

    <form onsubmit="return submitForm('#myForm');" id='myForm'>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)