1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Problem in php ajax post value.

Discussion in 'PHP' started by youlichika, Dec 6, 2010.

  1. #1
    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:
     
    youlichika, Dec 6, 2010 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    $name = '<div id="msg">'.$_POST["user_name"].'</div>'; ?
     
    tvoodoo, Dec 6, 2010 IP
  3. imocoder

    imocoder Member

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #3
    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:
     
    imocoder, Dec 7, 2010 IP
  4. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    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.
     
    youlichika, Dec 7, 2010 IP
  5. imocoder

    imocoder Member

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #5
    Then try this:

    
    <?php
    echo $name="<div id='msg'>". $namea . " ".  $nameb."</div>";
    ?>
    
    Code (markup):
    Is this what you want?
     
    imocoder, Dec 7, 2010 IP
  6. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    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.
     
    youlichika, Dec 7, 2010 IP
  7. moneywant

    moneywant Peon

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    yah right :)
     
    moneywant, Mar 16, 2011 IP