0

I got stuck when using ajax as primary request sender to php.

I was trying to send data to php file and then take that data to view in another file, but the problem is I don't know how to do it .

I tried

var ajax = ajaxObj("POST", "function.php");
ajax.onreadystatechange = function() {
 if(ajaxReturn(ajax) == true) {
 window.location = "view.php";
 }
}
ajax.send("a="+a+"&b="+b); 

it locate view.php and I want to take contents that sent function.php and view it's contents in view.php.

That is where my problem occurs.

So, my question is HOW TO TAKE THOSE CONTENTS

jh314
27.9k16 gold badges66 silver badges83 bronze badges
asked Apr 21, 2015 at 16:30
1

2 Answers 2

1

I think you don't understand the use of ajax... here you don't need ajax at all. Ajax is used to update/send content without refreshing the page. So if you want to pass data to a new php file, just pass the variables directly to view.php in your case.

For instance:

go to view.php?a=[your value]&b=[your value]

in view.php you can get the values:

$a = $_GET['a'];
$b = $_GET['b'];

and pass it to your function.php

answered Apr 21, 2015 at 16:34
Sign up to request clarification or add additional context in comments.

Comments

0

Use Jquery to avoid dealing with browsers specifications and also jquery is pretty popular and simple to use library

Assuming you are familiar with php and JavaScript

first you have to do all this work in you www or htdocs folder depending on the stack you are using whether it is XAMPP,WAMP or LAMP

let's say as an example we want to send post request to function.php file and that file should verify the existance of $_POST['username'] and return the length of username otherwise it returns 0

so the function.php file should be something like this :

if(isset($_POST['username'])){
 echo strlen($_POST['username']);
}else{
 echo 0;
}

and the view.php file is the file used to send post request and get the response and displays it in a #display div

so here is the content of view.php

// first you should include jquery 
// here is a cdn link 
// https://code.jquery.com/jquery-2.1.3.min.js
// or you can use a local jquery file it is up to you
<div id="display">
</div>
// here is the simple js code to send and get the data from function.php file
<script>
$(document).ready(function(){
// waiting for the document until it is ready to use 
$.post('function.php',{username:'myusername'},function(data){
 // data here is the returned data from the function.php file 
 $('#display').text('username length is : '+data);
});
});
</script>
answered Apr 23, 2015 at 22:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.