I want to 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. then use them in other php part. 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. 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> PHP: value.php <?php echo $_POST["user_name"]; ?> PHP:
Try this code: <?php if(isset($_POST['user_name'])) { $name1=strip_tags($_POST['user_name']); $name2 = explode("|",$name1); $namea=$name2[0]; $nameb=$name2[1]; echo "1: $namea\n"; echo "2: $nameb\n"; return; } ?><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>"; ?> <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> PHP:
Thanks imocoder, but my purpose is to manipulate $namea, $nameb. it is not very necessary to need a value.php. as your code, I change var url = "value.php"; Code (markup): to var url = "index.php"; Code (markup): then <?php if(isset($_POST['user_name'])) { $name1=strip_tags($_POST['user_name']); $name2 = explode("|",$name1); $namea=$name2[0]; $nameb=$name2[1]; echo "1: $namea\n"; echo "2: $nameb\n"; return; } ?> <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 = "ajax.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>"; echo $namea; echo $nameb; ?> <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> Code (markup): but if I set a echo $namea; echo $nameb; out of the <div id='msg'></div> , it become empty. how to do that? thanks again.
Then try this: <?php echo $name="<div id='msg'>". $namea . " ". $nameb."</div>"; ?> Code (markup): Is this what you want?
no, I want to deal with the php variables, like <ul> <?php $result = mysql_query("SELECT * FROM yokohama WHERE tag='".$namea.""); while ($row = mysql_fetch_array($result)) { echo '<li><span>'.$row['title'].'</span></li>'; } ?> </ul> Code (markup): I will use $namea and $nameb in many section of my code. In fact, I have a group of buttons like <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> Code (markup): I want click each of them, without refresh the page(the page is base on a thickbox iframe child page, if refresh, the page will be closed and all the values will be lsot), output different values for php database process.