Hi

I have a function which either updates or inserts a record into the database table, depending on what the user selects.

However I need to manually refresh the page after the action has been done, as the status is not updating on the page.

How can I force a refresh of the page directly after either the UPDATE or INSERT has been done?

Many thanks for the help

///// functions ///// 


function update_shortlist($prop_id,$user_id,$new_status)
{ 
	$prop_id = (Int)$prop_id;
	$user_id = (Int)$user_id;

// Connect to the database
  	$dbc = mysqli_connect("localhost", "xxxxxxxxx", "xxxxxxxxx", "xxxxxxxx");	

// Check to see if the item is already on the short list
	$query_chkStatus = "SELECT status FROM DEV_property_shortlist WHERE DEV_property_shortlist.prop_id=$prop_id AND DEV_property_shortlist.ID = $user_id";
	$chkStatus = mysqli_query($dbc, $query_chkStatus) or die(mysql_error());
	$chkRows = mysqli_num_rows($chkStatus);
	
	if ($chkRows > 0) {
		// Update Existing Row
		$query = "UPDATE `DEV_property_shortlist` SET status= '$new_status', `add_date` = Now() WHERE DEV_property_shortlist.prop_id=$prop_id AND DEV_property_shortlist.ID = $user_id"; 	 	
   		mysqli_query($dbc, $query) or die(mysql_error());
		
	}
	else {
		if ($new_status = 'Shortlisted') {
		// Insert New Row
		$query = "INSERT INTO `DEV_property_shortlist`(`prop_id`, `ID`, `status`, `add_date`) VALUES ($prop_id,$user_id,'Shortlisted',Now())";   		
		mysqli_query($dbc, $query) or die(mysql_error());
		
		}
	}

	mysqli_close($dbc);
} 

?>

Following line might help you

echo "<script>window.location.reload();</script>";

Hi Thanks for getting back to me.
Where do I put this? I tried placing it after the mysqli_query() but it just made the page freeze and nothing happened. I think it should be directly after the INSERT and UPDATE though?

many thanks

Is it an Ajax request which calls the update_shortlist($prop_id,$user_id,$new_status)? If so put it in the onreadystatechange as follows

xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        window.location.reload();
    }
}

If not tell me about the flow of the program to get an idea

Hi
No it is not Ajax. Just PHP.

Here is the link itself:

<a href="?run=add_shortlist&amp;recordID=<?php echo $_GET['recordID']?>">Add To Shortlist</a>

It calls this:

<?php 
// this decides what to put in the menu for add or remove from shortlist

	if (isset($_GET['run'])) {$linkchoice=$_GET['run'];} else {$linkchoice='';} 
		switch($linkchoice){
			case 'add_shortlist' : 
			update_shortlist($_GET['recordID'],$_SESSION['ID'],'Shortlisted');
			break;
			case 'remove_shortlist' :
			update_shortlist($_GET['recordID'],$_SESSION['ID'],'Removed');
			break;
			default : 		}
 ?>

Which in turn carries out the appropriate part of the function above. :0)

So the link is referring to the same page right? Seems like you do not need a manual refresh because clicking the link reloads the page.

So I believe the format of the page is like this:

<?php
	// this decides what to put in the menu for add or remove from shortlist	 
	if (isset($_GET['run'])) {$linkchoice=$_GET['run'];} else {$linkchoice='';}
	switch($linkchoice){
		case 'add_shortlist' :
			update_shortlist($_GET['recordID'],$_SESSION['ID'],'Shortlisted');
			break;
		case 'remove_shortlist' :
			update_shortlist($_GET['recordID'],$_SESSION['ID'],'Removed');
			break;
		default : 
	}
	
	// Since you are displaying data in the page you have to fetch data from the database
	// Here you write the query to fetch displaying data
?>

<!-- Here you display the data -->

<!-- Then the link -->
<a href="?run=add_shortlist&amp;recordID=<?php echo $_GET['recordID']?>">Add To Shortlist</a>

EDIT: If my above assumption about the display is correct, actually you do not need a manual reload, because data is fetched to display after updating the database.

Hi Yes. That is correct. It should be refreshing itself. But for some reason it is not. So the data gets updated or inserted when the link is clicked, but the data is not updating and displaying back to the user on the page. I have to right-click and 'refresh' for it to show.

I agree should be happening already??

Are you sure that data fetching code and the data saving code are in the following order:

1st Data saving
2nd Data fetching
3rd Data Displaying

OK I'll check and get back to you.

thanks!

Welcome :)

Hi
Still not having too much luck.
The following code comes directly after the SQL SELECT Query
Basically if the user clicks on 'add_to_shortlist' link - it updates the database table, but you have to click the link a second time for the status to echo back to the screen.
I think it might be because the database is updated quickly, but the information coming back to the client-side is too slow..

I don't know what to do to get the status to immediately show? I could direct the link to a new page which would solve the problem, but I don't really want to do this...?

<?php 

///// functions ///// 

// this decides what to put in the menu for add or remove from shortlist

	if (isset($_GET['run'])) {$linkchoice=$_GET['run'];} else {$linkchoice='';} 
		switch($linkchoice){
			case 'add_shortlist' : 
			update_shortlist($_GET['recordID'],$_SESSION['ID'],'Shortlisted');
			break;
			case 'remove_shortlist' :
			update_shortlist($_GET['recordID'],$_SESSION['ID'],'Removed');
			break;
			default : 		}
 ?> 

<?php
function update_shortlist($prop_id,$user_id,$new_status)
{ 
	$prop_id = (Int)$prop_id;
	$user_id = (Int)$user_id;

// Connect to the database
  	$dbc = mysqli_connect("localhost", "xxxxxxx", "xxxxxxxx", "xxxxxxxxx");	

// Check to see if the item is already on the short list
	$query_chkStatus = "SELECT status FROM DEV_property_shortlist WHERE DEV_property_shortlist.prop_id=$prop_id AND DEV_property_shortlist.ID = $user_id";
	$chkStatus = mysqli_query($dbc, $query_chkStatus) or die(mysql_error());
	$chkRows = mysqli_num_rows($chkStatus);
	
	if ($chkRows > 0) {
		// Update Existing Row
		$query = "UPDATE `DEV_property_shortlist` SET status= '$new_status', `add_date` = Now() WHERE DEV_property_shortlist.prop_id=$prop_id AND DEV_property_shortlist.ID = $user_id"; 	 	
   		mysqli_query($dbc, $query) or die(mysql_error());
		
		
	}
	else {
		if ($new_status = 'Shortlisted') {
		// Insert New Row
		$query = "INSERT INTO `DEV_property_shortlist`(`prop_id`, `ID`, `status`, `add_date`) VALUES ($prop_id,$user_id,'Shortlisted',Now())";   		
		mysqli_query($dbc, $query) or die(mysql_error());
		
		
		}
	}

	mysqli_close($dbc);
} 

?>

Can you post the whole php file which contains both database accessing and html view? At least the sections related to this problem?

Hi
Many thanks for taking a look at this....

Here is the mysql query and the functions:

mysql_select_db($database_xxxxxx_localhost, $xxxxxxx_localhost);
$query_rs_propdetails = sprintf("SELECT DEV_property_details.prop_id, DEV_property_details.add_owner, DEV_property_details.prop_contact, DEV_property_details.prop_desc, DEV_property_details.prop_garage, DEV_property_details.prop_park, DEV_property_details.prop_pool, DEV_property_details.prop_garden, DEV_property_details.prop_alarm, DEV_property_details.prop_appliance, DEV_property_details.screenpath, DEV_property_details.image_main, DEV_property_details.image_kitchen, DEV_property_details.image_bed, DEV_property_details.image_bath, DEV_property_details.image_living, DEV_property_details.image_dining, DEV_property_details.image_garden, DEV_user_registration.ID, DEV_user_registration.reg_email, DEV_property_details.ID, DEV_user_registration.reg_agent, DEV_property_details.prop_cat, DEV_property_details.prop_type, DEV_property_details.add_zone, DEV_property_details.prop_saletype, DEV_property_details.add_date, DEV_property_details.prop_size, DEV_property_details.add_area, DEV_user_registration.reg_lname, DEV_user_registration.reg_fname, DEV_user_registration.reg_companyname, DEV_user_registration.reg_tel, DEV_user_registration.reg_email, DEV_user_registration.screenpath, DEV_user_registration.reg_image, DEV_property_details.add_road, DEV_property_details.add_house, DEV_property_details.add_unit, DEV_property_details.add_road, DEV_property_details.add_postcode, DEV_property_details.prop_price, DEV_property_details.prop_bedroom, DEV_property_details.prop_bathroom, DEV_property_details.prop_tenure, DEV_property_shortlist.status
FROM DEV_user_registration, DEV_property_details
LEFT JOIN DEV_property_shortlist ON DEV_property_details.prop_id = DEV_property_shortlist.prop_id 
WHERE DEV_property_details.ID = DEV_user_registration.ID AND DEV_property_details.prop_id = %s AND DEV_property_shortlist.ID = '$user_id'", GetSQLValueString($colname_rs_propdetails, "int"));
$rs_propdetails = mysql_query($query_rs_propdetails, $dreamsin_localhost) or die(mysql_error());
$row_rs_propdetails = mysql_fetch_assoc($rs_propdetails);
$totalRows_rs_propdetails = mysql_num_rows($rs_propdetails);


$email = $row_rs_propdetails['reg_email'];
$subject = $row_rs_propdetails ['prop_id'];
?>

<?php 

///// functions ///// 

// this decides what to put in the menu for add or remove from shortlist

	if (isset($_GET['run'])) {$linkchoice=$_GET['run'];} else {$linkchoice='';} 
		switch($linkchoice){
			case 'add_shortlist' : 
			update_shortlist($_GET['recordID'],$_SESSION['ID'],'Shortlisted');
			break;
			case 'remove_shortlist' :
			update_shortlist($_GET['recordID'],$_SESSION['ID'],'Removed');
			break;
			default : 		}
 ?> 

<?php
function update_shortlist($prop_id,$user_id,$new_status)
{ 
	$prop_id = (Int)$prop_id;
	$user_id = (Int)$user_id;

// Connect to the database
  	$dbc = mysqli_connect("localhost", "xxxxxxx", "xxxxxxxx", "xxxxxxx_property");	

// Check to see if the item is already on the short list
	$query_chkStatus = "SELECT status FROM DEV_property_shortlist WHERE DEV_property_shortlist.prop_id=$prop_id AND DEV_property_shortlist.ID = $user_id";
	$chkStatus = mysqli_query($dbc, $query_chkStatus) or die(mysql_error());
	$chkRows = mysqli_num_rows($chkStatus);
	
	if ($chkRows > 0) {
		// Update Existing Row
		$query = "UPDATE `DEV_property_shortlist` SET status= '$new_status', `add_date` = Now() WHERE DEV_property_shortlist.prop_id=$prop_id AND DEV_property_shortlist.ID = $user_id"; 	 	
   		mysqli_query($dbc, $query) or die(mysql_error());
		
		
	}
	else {
		if ($new_status = 'Shortlisted') {
		// Insert New Row
		$query = "INSERT INTO `DEV_property_shortlist`(`prop_id`, `ID`, `status`, `add_date`) VALUES ($prop_id,$user_id,'Shortlisted',Now())";   		
		mysqli_query($dbc, $query) or die(mysql_error());
		
		
		}
	}

	mysqli_close($dbc);
} 

?>

And here is the part of the document which has the links:

<?php 
					if (!empty ($_SESSION['ID']) && $row_rs_propdetails ['status'] == "Removed" || $row_rs_propdetails ['status'] == "")   
					{
						echo "<a href=?run=add_shortlist&amp;recordID=" . $_GET['recordID'] . ">Add To Shortlist</a>" ;
                     } 
					 ?>
                    </td>
                   </tr>
                    <tr>
                      <td align="left" class="maintext">
                      <?php 
					  if (!empty ($_SESSION['ID']) && $row_rs_propdetails ['status'] == "Shortlisted" || $row_rs_propdetails ['status'] == "") 
					  {
					  echo "<a href=?run=remove_shortlist&amp;recordID=" . $_GET['recordID'] . ">Remove from Shortlist</a>";
                     } ?>

Currently I just want to echo the status in the same way as all the other fields on the page:

<?php echo $row_rs_propdetails['status']; ?>

Try switching the places of you php code snippets like this:

<?php
 mysql_select_db($database_xxxxxx_localhost, $xxxxxxx_localhost);
///// functions /////
 
// this decides what to put in the menu for add or remove from shortlist
 
if (isset($_GET['run'])) {$linkchoice=$_GET['run'];} else {$linkchoice='';}
switch($linkchoice){
case 'add_shortlist' :
update_shortlist($_GET['recordID'],$_SESSION['ID'],'Shortlisted');
break;
case 'remove_shortlist' :
update_shortlist($_GET['recordID'],$_SESSION['ID'],'Removed');
break;
default : }
?> 

<?php

$query_rs_propdetails = sprintf("SELECT DEV_property_details.prop_id, DEV_property_details.add_owner, DEV_property_details.prop_contact, DEV_property_details.prop_desc, DEV_property_details.prop_garage, DEV_property_details.prop_park, DEV_property_details.prop_pool, DEV_property_details.prop_garden, DEV_property_details.prop_alarm, DEV_property_details.prop_appliance, DEV_property_details.screenpath, DEV_property_details.image_main, DEV_property_details.image_kitchen, DEV_property_details.image_bed, DEV_property_details.image_bath, DEV_property_details.image_living, DEV_property_details.image_dining, DEV_property_details.image_garden, DEV_user_registration.ID, DEV_user_registration.reg_email, DEV_property_details.ID, DEV_user_registration.reg_agent, DEV_property_details.prop_cat, DEV_property_details.prop_type, DEV_property_details.add_zone, DEV_property_details.prop_saletype, DEV_property_details.add_date, DEV_property_details.prop_size, DEV_property_details.add_area, DEV_user_registration.reg_lname, DEV_user_registration.reg_fname, DEV_user_registration.reg_companyname, DEV_user_registration.reg_tel, DEV_user_registration.reg_email, DEV_user_registration.screenpath, DEV_user_registration.reg_image, DEV_property_details.add_road, DEV_property_details.add_house, DEV_property_details.add_unit, DEV_property_details.add_road, DEV_property_details.add_postcode, DEV_property_details.prop_price, DEV_property_details.prop_bedroom, DEV_property_details.prop_bathroom, DEV_property_details.prop_tenure, DEV_property_shortlist.status
FROM DEV_user_registration, DEV_property_details
LEFT JOIN DEV_property_shortlist ON DEV_property_details.prop_id = DEV_property_shortlist.prop_id
WHERE DEV_property_details.ID = DEV_user_registration.ID AND DEV_property_details.prop_id = %s AND DEV_property_shortlist.ID = '$user_id'", GetSQLValueString($colname_rs_propdetails, "int"));
$rs_propdetails = mysql_query($query_rs_propdetails, $dreamsin_localhost) or die(mysql_error());
$row_rs_propdetails = mysql_fetch_assoc($rs_propdetails);
$totalRows_rs_propdetails = mysql_num_rows($rs_propdetails);
 
 
$email = $row_rs_propdetails['reg_email'];
$subject = $row_rs_propdetails ['prop_id'];
?>

Make sure when you switching the places, that data base connection related code lines to be there as it is. (Ex: mysql_connect and mysql_select_db should be there at the top as it is now)

Hi
That works beautifully!
I put only the 1st function at the top of the page right under:

<?php require_once('Connections/xxxxxxx_localhost.php');

Many thanks!

Hi - how can I up-vote you? :-)

Great..!!! :) :)
There is an arrow in each of my replies (top right corner). Click to up-vote :) :)

EDIT: Make sure you click the correct arrow :) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.