开发者

How to give a PHP variables in this situation?

开发者 https://www.devze.com 2023-01-28 22:19 出处:网络
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, how to

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, how to give a PHP variables in it? for example: $name= <div id="msg"></div> I tried to use includ('value.php') and combine two pages, put

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

in <div id="msg"></div>, they are all not correct method. How to do? and if there has other method can post post value in self page without refresh the page. pls teach me. thax.

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"); 
          开发者_Go百科       } 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) { 
    msg.innerHTML = ajax.responseText; 
   } 
} 
} 
</script> 
</head> 
<body > 
<div id="msg"></div> 
<form name="user_info" id="user_info" method="post"> 
<input name="user_name" type="hidden" value="abc" /><br /> 
<input type="button" value="abc" onClick="saveUserInfo()"> 
</form> 
</body> 

value.php

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


$name= "<div id='msg'></div>"; give that a try

0

精彩评论

暂无评论...
验证码 换一张
取 消