weixin_33686714 2010-12-06 20:57 采纳率: 0%
浏览 25

PHP Ajax发布价值问题

I want to make a php ajax post.(post value without refresh the page) here is my code. It can return the value and show in <div id="msg"></div>, But I also want to use this value.

In @benhowdle89 's help, I made $name= "<div id='msg'></div>". but when I use echo $name, in the source code, I can see <div id='msg'></div>(html tag), this is not a pure value, so I tried to use strip_tags, but the value lost. it seems the left the ajax pointed div tag, the value also gone. Still waiting for help...

index.php

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script language="javascript"> 
function saveUserInfo() { 
    var msg = document.getElementById("msg"); 
    var f = document.user_info; 
    var userName = f.user_name.value; 
    var url = "value.php"; 
    var postStr   = "user_name="+ userName; 
    var ajax = false; 

    if(window.XMLHttpRequest) { 
        ajax = new XMLHttpRequest(); 
        if (ajax.overrideMimeType) { 
            ajax.overrideMimeType("text/xml"); 
        } 
    } else if (window.ActiveXObject) { 
        try { 
            ajax = new ActiveXObject("Msxml2.XMLHTTP"); 
        } catch (e) { 
            try { 
                ajax = new ActiveXObject("Microsoft.XMLHTTP"); 
            } catch (e) { 
            } 
        } 
    } 

    if (!ajax) { 
        window.alert("wrong"); 
        return false; 
    } 

    ajax.open("POST", url, true); 
    ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    ajax.send(postStr); 
    ajax.onreadystatechange = function() { 
        if (ajax.readyState == 4 && ajax.status == 200) { 
            var myPhpVariable = ajax.responseText; 
            msg.innerHTML = myPhpVariable; 
            // myPhpVariable is now a variable which you can use
            alert( myPhpVariable ); 
        } 
    } 
} 
</script> 
</head> 
<body>
<?php
echo $name="<div id='msg'></div>";
$name1=strip_tags($name);
$name2 = explode("|",$name1);
$namea=$name2[0];
$nameb=$name2[1];
?>
<form name="user_info" id="user_info" method="post"> 
<input name="user_name" type="hidden" value="abc|def" /><br /> 
<input type="button" value="abc|def" onClick="saveUserInfo()"> 
</form> 
</body> 

value.php

<?php 
echo $_POST["user_name"]; 
?> 

This is what I want. post value from index.php, then get the value by self without refresh the page. one botton with two values, I want explode them and finally get $namea and $nameb. I want use them in other php part.

  • 写回答

2条回答 默认 最新

  • 北城已荒凉 2010-12-06 21:25
    关注

    by including $name= "<div id='msg'></div>" and calling echo $name, you're just telling the program to store "" in the $name variable and then print what is stored in that variable. That's why you're getting the unwanted output.

    not sure if you're having problems posting the value or showing it in the value, but you need to echo the variable where the userName is stored, may need to send that from the ajax to the php and set it to $name.

    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀