Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3459342
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:05:41+00:00 2026-05-18T10:05:41+00:00

I want to make a php ajax post.(post value without refresh the page) here

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:05:41+00:00Added an answer on May 18, 2026 at 10:05 am

    You can put the ajax response into a javascript variable, then you can manipulate it from there:

    var myPhpVariable = ajax.responseText;
    msg.innerHTML = myPhpVariable;
    alert( myPhpVariable );
    

    Here is a working javascript example (full code):

    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 );
            }
        }
    }
    

    The PHP file would look like:

    $postVar = $_POST["user_name"];
    $postVarArr = explode('|', $postVar);
    
    // will show abc
    //echo $postVarArr['0'];
    
    // will show def
    echo $postVarArr['1'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a php ajax post.(post value without refresh the page) here
I am using php/ajax to submit a form without page refresh. Here are my
I want to make sure people can't type the name of a PHP script
I got this script from here $(document).ready(function() { $('#choose').change(function(event) { $.post('select-ajax.php', { selected: $('#choose').val()
In there an easy way to do this in PHP. I want to make
I am trying to use jQuery to make a synchronous AJAX post to a
Here is my jQuery code: <script type=text/javascript> //<!-- $(document).ready(function() { $('input[type=submit]').click(function() { $.ajax({ type:
I want to write a javascript function, that will make an ajax request to
For example, when I want to update a part of my page with AJAX
Hello i want in the moment i select a value to post to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.