dqzg62440 2016-01-19 04:51
浏览 31
已采纳

如何使用AJAX(而不是Jquery AJAX)发送参数并使用PHP接收它

I'm trying send some AJAX(not Jquery AJAX) parameter to a server with HTML form values and store it a database. But it doesn't work correctly. When i hit the Let's start button i have got my error message Here is my code....

<div class ="col-xs-12 col-md-12 col-sm-12 col-lg-12" id ="AJAXsignup">
        <h3>Creating a new account...</h3>

        <input class ="form-control" type ="text" name ="fullname" placeholder ="Type your name eg:-Jhon Smith" id ="namefull">
        <span class ="label label-warning" id ="err_fullname">You should type your name</span>

        <input class ="form-control" type ="text" name ="username" placeholder ="Type your user name eg:-Jhon 95" id ="nameuser" style ="margin-top:5px;">
        <span class ="label label-warning" id ="err_username">Missing username</span>

        <div class ="input-group">
        <span class ="input-group-addon">@</span>
        <input class ="form-control" type ="email" name ="email" placeholder ="Type your Email" id ="email" style ="margin-top:5px;">
        <span class ="label label-warning" id ="err_email">Missing email</span>
        </div>

        <input class ="form-control" type ="password" name ="password" placeholder ="Type your Password" id ="pass" style ="margin-top:5px;">
        <span class ="label label-warning" id ="err_password">Missing Password</span>

        <input class ="form-control" type ="password" name ="password" placeholder ="Repeat your Password" id ="repass" style ="margin-top:5px;">
        <span style ="margin-bottom:5px;" class ="label label-warning" id ="err_repass">Repeat Password</span>

        <img class ="col-sm-offset-5 col-md-offset-5 col-lg-offset-5" src ="images\Preloader_2.gif" id ="loader" style ="display:none;" style ="margin-bottom:5px;">

        <input class ="btn btn-lg btn-info col-md-12 col-lg-12 col-sm-12 col-xs-12" type ="submit" id ="btn" value ="Let's start!" style ="margin-bottom:5px;" onclick = "validate()">

        <input class ="btn btn-lg btn-danger col-md-12 col-lg-12 col-sm-12 col-xs-12"type ="button" id ="frm-can" value ="No.Thanks">

    </div>

The JAVASCRIPT code

function sendinginfomation(){
    if(window.ActiveXObject){
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        xmlhttp = new XMLHttpRequest();
    }
    var fullname = document.getElementById('namefull').value;
    var user_n = document.getElementById('nameuser').value;
    var email = document.getElementById('email').value;
    var user_p = document.getElementById('pass').value;
    xmlhttp.open("GET",'php/signup.php?w='+fullname+"&n_p="+user_n+"&tv="+email+"&q="+user_p,true);
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState ==4){
            $("#loader").fadeOut(300);
            document.getElementById('AJAXsignup').innerHTML = xmlhttp.responseText;
            }
    }
    xmlhttp.send(null);
}

The PHP code

$fullname = $_GET['w'];
$username = $_GET['n_p'];
$email = $_GET['tv'];
$password = $_GET['q'];
$server = "localhost";
$username = "root";
try{
    $conn = new PDO("mysql:host=$server;dbname=reg_mem",$username);
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO mem_info(Full_name,User_name,Email,Password) VALUES($fullname,$username,$email,$password)";
    $conn->exec($sql);
    echo "The infomation sent sunncessfully.";
    }catch(PDOException $e){
    echo "The infomation unable to send right now";
    }
  • 写回答

5条回答 默认 最新

  • drake900918 2016-01-19 05:00
    关注

    Use Jquery ajax to send var to the PHP page.

    $.ajax({
            type: "GET", (or POST)
            url: "code.php",
            data: { fullname, user_n, email },
            cache: false,
            success: function(){
          }
        });
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)