Home > Backend Development > PHP Tutorial > 初学script+ajax 数据提交问题

初学script+ajax 数据提交问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:28:44
Original
1132 people have browsed it

HTML中的内容
............
        

密码:
            ">
        

        
邮箱:
            ">
        

............


<script> <br /> function submit(i) <br /> { <br /> var sendData=""; <br /> if(i=="1"){sendData='{"action":"'+i+'","pw":'+getElementByName+',"email":'+getElementByName+':}';} <br /> $.ajax({ <br /> timeout:5000, <br /> type:"POST", <br /> url:"edit_server.php", <br /> data:sendData, <br /> success:function(data) <br /> { <br /> getData(1); //这是一个函数,用来打开一个页面。 <br /> } <br /> }); <br /> } <br /> </script>



edit_server.php中的内容:

require_once "functions.php";
connectDb();
if($_GET['action']=="1")
{
    $pw = $_POST['pw'];
    $email = $_POST['email'];
    mysql_query("UPDATE basic_information SET pw = '$pw',email = '$email');
    if(mysql_errno()){
        echo mysql_error();
    }else{
        getData(1);
    }
}


回复讨论(解决方案)

几个问题:
1、edit_server.php 中要检查 $_GET['action']=="1"
     但 ajax 的 url 中没有参数(url:"edit_server.php",)
     要这样 url:"edit_server.php?action=" + i, 
2、sendData='{"action":"'+i+'","pw":'+getElementByName+',"email":'+getElementByName+':}'
     这样赋值后 sendData 是字符串,不会被 php 解析成 $_POST 数组
     要这样

sendData = {pw : document.getElementByName(‘pw').value, email : document.getElementByName('emal'][0].value }
Copy after login

前端ajax  提交时用了post方式  ,后端PHP 用    if( $_GET['action']=="1") { ....}  来验证所以问题就来了。 

修改后


<script> <br /> function submit(i) <br /> { <br /> var sendData=""; <br /> if(i=="1"){sendData={pw:getElementByName("pw").value,email:getElementByName("email").value};} <br /> $.ajax({ <br /> timeout:5000, <br /> type:"POST", <br /> url:"edit_server.php?action="+i, <br /> data:sendData, <br /> success:function(data) <br /> { <br /> alert(data); <br /> getData(1); <br /> } <br /> }); <br /> } <br /> </script>


后台:

if($_POST['action']=="1")
{
    $pw = $_POST['pw'];
    $email = $_POST['email'];
    mysql_query("UPDATE basic_information SET pw = '$pw',email = '$email');
    if(mysql_errno()){
        echo mysql_error();
    }else{
        getData(1);
    }
}

但依然无法运行,控制台提示ReferenceError: getElementByName is not defined

getElementsByName

改完后,数据依然没有传入

换种ajax方式,初始化后,在提交,发送参数,接收返回数据。

非常感谢诸位大神,已成功解决问题。
   if(i=="1"){sendData={pw: document.getElementByName("pw").value,email: document.getElementByName("email").value};}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template