0

I have main file index.php. Where user perform login I load url.php in <div> inside index.php through ajax request.

Now on url.php, on button click event I submit the form and post data to data.php file. As url.php is loaded into index.php, button click event on url.php will call function in section of index.php. I am getting two variable $x and $y as a result on data.php.

On index.php I have JS function where I want to use $x and $y. How can I retrieve?

index.php

 <script type="text/javascript">
    //------------------script 2 starts ---------
    function showUser(form, e) {
      //alert(myid);
      e.preventDefault();
      e.returnValue=false;
      var xmlhttp;
      var sent = form.elements['sent'].value;
      //var sent2 = form.elements['sent2'].value;
      //alert(sent2);
      //var divElements = document.getElementById('d1').innerHTML;
      var text1 = document.getElementById('previewUrl').innerText || document.getElementById('previewUrl').textContent;
      var text2 = document.getElementById('previewTitle').innerText || document.getElementById('previewTitle').textContent;
      var text3 = document.getElementById('previewDescription').innerText || document.getElementById('previewDescription').textContent;
      //alert(text3);      
      console.log(sent);

      if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
      }
      else {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange = function(e) {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
          document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
      }
        xmlhttp.open(form.method, form.action, true);
      xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');


      sent = encodeURIComponent(sent);
      text1 = encodeURIComponent(text1);
      text2 = encodeURIComponent(text2);
      text3 = encodeURIComponent(text3);

      xmlhttp.send('sent=' + sent + '&text1=' + text1 + '&text2=' + text2 + '&text3=' + text3);
    }

  </script>

+

$.ajax({
                         url:'url.php'
                         ,async:     true
                         ,type : 'POST'
                         ,cache:     false
                         ,data : 'myid=' + myid
                         ,dataType:  'html'
                         ,success:   function(data){
                           $('body').html(data);
                           FB.XFBML.parse();
                         }
                       }
                             );

url.php

<form action="data.php" method="POST" onsubmit="showUser(this, event)">

data.php

 if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Desktop/test/b '$test'"),  $matches)){ //Values stored in ma. 
    echo "hi";    
    $x = (int) $matches[1][0]; //optionally cast to int
    $y = (int) $matches[1][1];

this x and y I want to fetch in index.php.

I know this is not impossible. But complex to thing for me!

3
  • when you say you load url.php, is it a ajax load or a frame inside the index.php? The page navigation is not very clear in the question. could you explain it very crisp as to how the events occur. something like this index.php -> ajax -> url.php (same window using iframe) -> form submit and so on Commented Oct 25, 2013 at 16:53
  • @nandu: I load it in <div> insdie index.php. Edited question Commented Oct 25, 2013 at 16:58
  • when you submit the page it would automatically move to the submitted page. you cannot have the values in the former page because it doesn't stay there. Its a different case if you are using frames or iframes though Commented Oct 25, 2013 at 17:37

2 Answers 2

1

My preferred method of sending data back from the server side is responding with JSON response..

$data['x'] = $x;
$data['y'] = $y;
header('Content-Type: application/json');
echo json_encode($data);

That should give a response to your ajax callback, so you could use it like this:

$.post('data.php', { "myid": id }, function(data) {
    console.log(data.x); //data.x = $x
    console.log(data.y); //data.y = $y
});

I didn't test the code to see if it actually works, but it should give an idea of what to do. Let me know if it makes sense :)

Sign up to request clarification or add additional context in comments.

2 Comments

good to know this. If I declare one global var in index.php. Then I assign above data.x to that variable inside function where I fetch it. After that can I use it globaly in any other function?
thanks bro. But please see. I data.php is as a action in form submit. Showuser() is the function where I post the value to data.php. How can I get data.x and data.y in showuser() function?
1

If you want to retrieve only two value. Simplest solution just like boiling a egg is:

just print both value in with separating value like ':'

echo $x.':'.$y;

and when you getting response. separate value with ':' and then you can use them..

or JSON response is also good practice for this.

2 Comments

Thanks but on that page alredy some othe echos! probably json will do
ya json is coolest way to do it. it also good for number of response attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.