Forum Moderators: open

Message Too Old, No Replies

php header and how to redirect with Ajax

         

helenp

9:30 am on Feb 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,
I have a php page, wich is called by Ajax.
The php script checks several things, if true some messages comes up but if false a redirection should be done, and header does not work.
In my ajax script nothing is returned,
its only open the php page and executes it.

After reading that headers arent possible with Ajax and that the redirection should be done inside Ajax, I been trying to do things like:


function available(response)
{
var divresponse = document.getElementById('response');
if (response == "Success!") {
window.location='https://www.mysite.com/page.php';
}
}

Does not work of course.
No idea how I can declare the response in my php page:

else{
?>
<div id="response">
<p>Success!</p>
</div>

<?php
}

daveVk

10:13 am on Feb 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try alert here

function available(response)
{
alert(response);
var divresponse = document.getElementById('response');
if (response == "Success!") {
window.location='https://www.mysite.com/page.php';
}
}

It is not clear if response is

Success!

or

<p>Success!</p>

or maybe

<div id="response">
<p>Success!</p>
</div>

helenp

10:26 am on Feb 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




It is not clear if response is

Success!

or

<p>Success!</p>

or maybe

<div id="response">
<p>Success!</p>
</div>

Thanks,
Not sure if you mean its not clear what I want as response.
I dont want any response, just need a silent redirection. Just added the div with <p>Success!</p> to try something as suppose response canīt be empty.


function available(response)
{
alert(response);
var divresponse = document.getElementById('response');
if (response == "Success!") {
window.location='https://www.mysite.com/page.php';
}
}
<div id="response">
<p>Success!</p>
</div>

Did not work, nothing happened at all.

daveVk

3:14 pm on Feb 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your test is

if (response == "Success!") {

I assume response is the PHP generated reply resulting from ajax request. For test to work red text needs to exactly match full PHP reply.

Did not work, nothing happened at all.


Do you have early version that works to any extent ?

helenp

4:32 pm on Feb 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your test is

if (response == "Success!") {

I assume response is the PHP generated reply resulting from ajax request. For test to work red text needs to exactly match full PHP reply.

Did not work, nothing happened at all.


Do you have early version that works to any extent ?

As I said,
no idea how I can declare response in my php.
In the php there is no response at all.
There is a several of conditions, if conditions are true a message is displayed, and works both with php and Ajax.
However if conditions are false in php a redirection should be done.
I only have an else nothing more.
This is the php:

if (!empty($percentage) && !empty($terms)) {
if ($salida > $llegada){
$query = $dbh->prepare("SELECT DATE_FORMAT(llegada, '%e %b %Y') as lleg, DATE_FORMAT(salida, '%e %b %Y') as sal,tiporeserva, propiedad, TO_DAYS(:salida) - TO_DAYS(:llegada) as dias from bookings where ( propiedad = :propiedad )
AND ((:llegada2 BETWEEN llegada AND date_sub(salida, interval +1 day))
or (:salida2 BETWEEN date_sub(llegada, interval -1 day) AND salida) or (llegada <= :llegada3 AND salida >= :salida3) or (llegada >= :llegada4 AND salida <= :salida4))");
$query->execute(array(
'salida' => $salida
, 'llegada' => $llegada
, 'propiedad' => $propiedad
, 'llegada2' => $llegada
, 'salida2' => $salida
, 'llegada3' => $llegada
, 'salida3' => $salida
, 'llegada4' => $llegada
, 'salida4' => $salida

));
$query->setFetchMode(PDO::FETCH_OBJ);
if ($row = $query->fetch()) {
if ($row->tiporeserva=="larga"){ echo "<p>This property is available only for long term rentals, consult us for a price.</p>
"; }
else {
$propiedad = str_replace("_", " ", $propiedad);
echo "<p>$propiedad is not available from $arrival_display to $departure_display.</p>";
echo "<p>$propiedad is occupied from ". $row->lleg ." until ". $row->sal ."</p>";
}

}//end if available query fetch
else{
?>
REDIRECTION SHOULD COME HERE

<?php
}//end else if available
}//end if salida mayor
else {echo "<span class=\"red\"><p>There is an error with the dates please try again, arrival: $arrival_display and departure: $departure_display</p> </span>";}
}//end if !empty form

else { echo "<p>Please fill in bookingoptions choosen (25% or 50%) and accept booking terms and conditions to proceed with the booking.</p>"; }
}
?>

helenp

4:47 pm on Feb 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



And this is the Ajax:


function objetoAjax(){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {

try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
function dataChanged() {
document.getElementById("resultado").style.display = "none";
}
function enviarDatosSolicitud(){
var divResultado = document.getElementById('resultado');
divResultado.style.display = "block";
propiedad = document.solicitud.propiedad.value;
terms = document.solicitud.propiedad.value;
percentage = document.solicitud.propiedad.value;
day = document.solicitud.day.options[document.solicitud.day.selectedIndex].value;
day2 = document.solicitud.day2.options[document.solicitud.day2.selectedIndex].value;
month = document.solicitud.month.options[document.solicitud.month.selectedIndex].value;
month2 = document.solicitud.month2.options[document.solicitud.month2.selectedIndex].value;

ajax=objetoAjax();

ajax.open("POST", "calculator.php",true);
ajax.onreadystatechange=function() {
if (ajax.readyState==4) {
divResultado.innerHTML = ajax.responseText
LimpiarCampos();
}
}
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send("propiedad="+propiedad+"&enviar="+enviar+"&day="+day+"&day2="+day2+"&month="+month+"&month2="+month2+"&percentage="+percentage+"&terms="+terms+"")
}

helenp

8:38 pm on Feb 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What about to check if the resultdiv is empty?

Been trying things like this with no success:


function dataChanged() {
document.getElementById("resultado").style.display = "none";
}
function enviarDatosSolicitud(){
var divResultado = document.getElementById('resultado');
divResultado.style.display = "block";


if (divResultado.length == 0)
{
window.location.href = '/myotherpage.php'
}




Also tried both:
if( $('#resultado').is(':empty') )
{
window.location.href = '/myotherpage.php'
}


if( $('#resultado:empty').length )
{
window.location.href = '/myotherpage.php'
}


Maybe its a question on where its placed?

helenp

9:07 pm on Feb 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Having a better thought.
If empty is no god as I also need to redirect if javascript is disabled.
This loosk ok "to me", but does not work.

The word available is printed out in the result as stated in the else stamente, but does not redirect:


function dataChanged() {
document.getElementById("resultado").style.display = "none";
}


function available()
{
document.getElementById('resultado');
if (resultado == "available") {
window.location='https://www.mysite.com/page.php';
}
}


function enviarDatosSolicitud(){
var divResultado = document.getElementById('resultado');
divResultado.style.display = "block";


This is what I have in the else in php and this is correctly printed on screen in the div with id resultado:

else{
echo "available";
}

daveVk

2:27 am on Feb 18, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



in PHP replace

REDIRECTION SHOULD COME HERE

with

echo 'redirect';

in javascript

if (ajax.readyState==4) {
divResultado.innerHTML = ajax.responseText
LimpiarCampos();
}

becomes

if (ajax.readyState==4) {
if ( ajax.responseText === "redirect" ) {
window.location='https://www.mysite.com/page.php';
} else {
divResultado.innerHTML = ajax.responseText
LimpiarCampos();
}
}

helenp

9:55 am on Feb 18, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




if (ajax.readyState==4) {
if ( ajax.responseText === "redirect" ) {
window.location='https://www.mysite.com/page.php';
} else {
divResultado.innerHTML = ajax.responseText
LimpiarCampos();
}
}

Thanks,
that works, and of course how easy to see and understand when its done correctly.....