noamkrief Posted November 1, 2007 Share Posted November 1, 2007 I have this ajax script that works with php mysql. When mysql gets 0 results, I want the page redirected. I find this impossible to do. Please if you could be thoughrough in your explination if you have my answer, I'm very new with ajax. html page: <head> <script language=Javascript> function Inint_AJAX() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript alert("XMLHttpRequest not supported"); return null; }; function dochange(src, val) { var req = Inint_AJAX(); req.onreadystatechange = function () { if (req.readyState==3) { document.getElementById('loading').innerHTML='<font id=loading>loading page...</font>'; } if (req.readyState==4) { document.getElementById('loading').innerHTML=''; if (req.status==200) { try { document.getElementById(src).innerHTML=req.responseText; //retuen value if (src == 'grab'){ fireMyPopup() } } catch(e) { //alert("Exception for " + src); } } } }; req.open("GET", "./connector.php?data1="+src+"&val="+val); //make connection req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header req.send(null); //send value } </head> Now I have the connection.php page which looks something like this: <?php // connect to db etc... $sql = mysql_query("some query"); if (mysql_num_rows($sql) > 0) { echo 'some text into the main index page'; } else { //need to redirect the index.html page to a different page... have tried doing header('location: ./page.html'); obviously that did not work... } ?> I cannot make the connector.php page force a browser redirection.... Any help would be greatly appreciated. thanks Link to comment https://forums.phpfreaks.com/topic/75683-help-redirecting-page-with-ajax/ Share on other sites More sharing options...
deadimp Posted November 2, 2007 Share Posted November 2, 2007 Make a block in Javascript that handles a certain flag returned by your AJAX request, and then either change the document location (better) or create a meta-refresh if you're paranoid (worse). Example: <ajax.php> $data=array('good'=>false, 'list'=>array()); if (mysql_numrows($res)) { $data['good']=true; //... Insert you data or whatever in the list } echo json_encode($data); <stuff.js> function ajax_handle(str) { //... Proper states, etc eval("var data="+str); //Ghetto JSON decode if (data.good) { //... Handle all the data } else document.location="redirect.php"; } That's the basic jist of it. Link to comment https://forums.phpfreaks.com/topic/75683-help-redirecting-page-with-ajax/#findComment-383235 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.