B.Matthieu
B.Matthieu

Reputation: 115

How to send data from a page to another page with ajax

I put a loop to get all my values of my database. Then, at the very left of each row there's a button to open another page to see all the database from a specific user. My problem here is that when you click on my button, there's a function that appears (function ouvrirEditFormulaire) and what I am trying to do is to get 4 values and to send it to that page : acceptation.php. So in my function I open a new window, then with ajax send my data to eFormulaire.php and I tried to innerHTML a div inside my other page (acceptation.php) but nothing happens. The most I could do is to innerHTML a div inside my first page (when you click on the button)...is there any way to send data to my acceptation.php ???

My AJAX :

function ouvrirEditFormulaire(id,date,heure,client) {
    window.open('concessionnaire/acceptation.php','mywin','left=20,top=20,width=650,height=730');
    var xhr = new XMLHttpRequest();
    xhr.open("POST","concessionnaire/eFormulaire.php",true);
    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4 && xhr.status == 200) {
               document.getElementById('divAnotherPage').innerHTML = xhr.responseText;
        }
    }
    xhr.send("id="+id+"&date="+date+"&heure="+heure+"&client="+client);
}

eFormulaire.php :

<?php
include_once('../connect.php');
$object = new User;
$id = $_POST['id'];
$date = $_POST['date'];
$heure = $_POST['heure'];
$client = $_POST['client'];
?>

Upvotes: 0

Views: 518

Answers (1)

Laoujin
Laoujin

Reputation: 10239

You don't need AJAX for this:

function ouvrirEditFormulaire(id,date,heure,client) { 
    var url = 'concessionnaire/acceptation.php';     
    window.open(url + "?id="+id+"&date="+date+"&heure="+heure+"&client="+client, 'mywin','left=20,top=20,width=650,height=730');
}

Right now you are opening a new page without parameters and then posting to the same page in a different request.

You will also have to chance to $_GET[xxx] on acceptation.php.

The AJAX call could be handy if you wanted to open the acceptation.php while remaining on the current page and show the results in some sort of dialog div.

Upvotes: 1

Related Questions