Page refresh not working with jQuery PHP?

Hi,
I have a jQuery function that posts information to the same page which then updates a mysql table using PHP. Everything is working OK including the update to the MySQL table.

But I can't seem to get the page to reload or refresh after the update has occured?
      
Not sure why the PHP header redirect  is not working (also echo statements are not displayed.)

Am I missing something, should I use PHP to reload the page or jQuery?

 
<?php

	if(isset($_POST['position'])){ 

	$page_id = $_POST['page_id'];
	//echo "Page ID posted".$page_id;
	$page_id_array = explode(",",$page_id);
	$count = 1;

	foreach($page_id_array as $key => $value){
		$update_new_pos = "UPDATE menu SET position = '".$count."' WHERE id = '".$value."'";
		//echo $query_icons."'\r\n";
		mysql_query($update_new_pos, $conn_data) or die(mysql_error());
		$count++;
	}

		   $pagereload = "menu_layodsadut.php";
		   header(sprintf("Location: %s", $pagereload));

	}
		
	?>
	
	<script>
		
	$(function() {
	$( "#leftmenu" ).sortable({'update':function(){
		position = new Array();
		page_id = new Array();
		$i = 0;
		$(".left-default").each(function(){
			position[$i] =  $i + 1;
			page_id[$i] = $(this).attr('page_id');
		$i++;
		});
			$.post("menu_layout.php", { position: ""+position+"", page_id: ""+page_id+""},
				function(data){
					//alert(data);
			});
	}});
	$( "#leftmenu" ).disableSelection();

	</script>

Open in new window

sabecsAsked:
Who is Participating?
 
hieloCommented:
>>I have a jQuery function that posts information to the same page which then updates a mysql table using PHP. Everything is working OK including the update to the MySQL table.
OK, upon review now I understand better what you are referring to.  I believe you are sending an AJAX post request via jquery.

When you do this, if you redirect to some other page (from the server via header), then the result that your jquery callback will see is the content of the page you redirected TO.  However, that does not mean that the current browser location will change. You need to do the redirection on the jquery callback function.
<?php

	if(isset($_POST['position'])){ 

	$page_id = $_POST['page_id'];
	//echo "Page ID posted".$page_id;
	$page_id_array = explode(",",$page_id);
	$count = 1;

	foreach($page_id_array as $key => $value){
		$update_new_pos = "UPDATE menu SET position = '".$count."' WHERE id = '".$value."'";
		//echo $query_icons."'\r\n";
		mysql_query($update_new_pos, $conn_data) or die(mysql_error());
		$count++;
	}

		   $pagereload = "menu_layodsadut.php";
		   // header(sprintf("Location: %s", $pagereload));
		   echo sprintf('{"location":"%s", "error":"0"}', $pagereload));
		   exit;

	}
		
	?>
	
	<script>
		
	$(function() {
	$( "#leftmenu" ).sortable({'update':function(){
		position = new Array();
		page_id = new Array();
		$i = 0;
		$(".left-default").each(function(){
			position[$i] =  $i + 1;
			page_id[$i] = $(this).attr('page_id');
		$i++;
		});
			$.post("menu_layout.php", { position: ""+position+"", page_id: ""+page_id+""},
				function(data){
					//alert(data);
					var result=$.parseJSON(data);
					if( result && result.error==0 )
					{
						location.href=result.location;
					}
			});
	}});
	$( "#leftmenu" ).disableSelection();

	</script>

Open in new window

0
 
hieloCommented:
try:
...
 header(sprintf("Location: %s", $pagereload));
 exit();
...
0
 
sabecsAuthor Commented:
Thanks Hielo for your help, I tried your suggestion but still not working.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Dushyant SharmaCommented:
the <?php directive has to be at the top of the page before anything else is written else header wont' work

if you have written anything above like

<doctype.... >
<?php
header directive
?>

then i think it is not going to work.

place the php block you have mentioned above at the very top of the page.
0
 
hexer4uCommented:
you can't use header(); if you have anything printed before it, even a space.
You can however do the following:
at line 1 of the file add <?php ob_start();?>
at last line of the file add <?php ob_end_flush(); ?>
Your header() redirect/refresh should work
0
 
Jagadishwor DulalBraces MediaCommented:
Try Using alternative way in this lines
 
$pagereload = "menu_layodsadut.php";
		   header(sprintf("Location: %s", $pagereload));

Open in new window


To
echo '<meta  http-equiv="refresh" content="0; url=menu_layodsadut.php">';

Open in new window

0
 
sabecsAuthor Commented:
Thanks hielo for your help.
I ended up just using the line location.href='menu_layout.php';
which appears to be working fine.

When I tried to add
var result=$.parseJSON(data);
if( result && result.error==0 ){
location.href='menu_layout.php';
}
I  received the following error

Message: Exception thrown and not caught
Line: 16
Char: 14229
Code: 0
URI: https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.