0

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>

value.php

<?php  
echo $_POST["user_name"];  
?>
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by saiprem
0

If you want to use any JS frameworks, check the following link to do your operation

http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

or if you want to use XMLHTTP objects then use this
http://www.pixel2life.com/publish/tutorials/221/introduction_to_php_5/

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>

value.php

<?php  
echo $_POST["user_name"];  
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.