0

Hi I have a PHP file with data. The value is passed on to another php file which process it successfully. But the first php file does not refresh to update the new result. It have to do it manually. Can any one tell me where I'm wrong or what needs to be done. Please find my code below.

PHP code (1st page, index.php)

function display_tasks_from_table() //Displayes existing tasks from table
{
 $conn = open_database_connection();
 $sql = 'SELECT id, name FROM todolist';

 mysql_select_db('todolist'); //Choosing the db is paramount

 $retval = mysql_query( $sql, $conn );
 if(! $retval )
 {
   die('Could not get data: ' . mysql_error());
  }
  echo "<form class='showexistingtasks' name='showexistingtasks' action='remove_task.php' method='post' >";

 while($row = mysql_fetch_assoc($retval))
{
 echo "<input class='checkbox' type='checkbox' name='checkboxes{$row['id']}' value='{$row['name']}' onclick='respToChkbox()' >{$row['name']}  <img src='images/show_options.gif' /><br>";       
} 
 echo "</form>";
 echo "<label id='removeerrormsg'></label>";
  close_database_connection($conn);
  }

Javascript code which finds the selected value:

var selVal; //global variable

function respToChkbox()
 {
var inputElements = document.getElementsByTagName('input'),
input_len = inputElements.length;

for (var i = 0; i<input_len; i++) 
{

    if (inputElements[i].checked === true)
    {
        selVal = inputElements[i].value;

    }

   }
  }

jQuery code which passes value to another page (remove_Task.php):

$(document).ready(function() {   
$(".checkbox").click(function(){
$.ajax({
type: "POST",
url: "remove_task.php", //This is the current doc
data: {sel:selVal, remsubmit:"1"},
success: function(data){
    //alert(selVal);
    //console.log(data);
}
});
});        
});

PHP code (2nd page, remove_task.php);

session_start();
error_reporting(E_ALL);ini_set('display_errors', 'On');


 $task_to_remove = $_POST['sel'];
 function remove_from_list() //Removes a selected task from DB
 {
$db_connection = open_database_connection();
global $task_to_remove;
mysql_select_db('todolist');
$sql = "DELETE FROM todolist WHERE name = "."'".$task_to_remove."'";
if($task_to_remove!='' || $task_to_remove!=null) 
{
mysql_query($sql, $db_connection);
}
    close_database_connection($db_connection);
    header("Location: index.php");
    }

     if($task_to_remove != "") {
     remove_from_list();
     }

The selected value is getting deleted but the display on index.php is not updated automatically. I have to manually refresh to see the updated result. Any help would be appreciated.

1 Answer 1

1

By calling header("Location: index.php"); you don't redirect main page. You sent an ajax request - you can think about it as of opening a new page at the background, so this code redirects that page to index.php.

The better way to solve your task is to return status to your success function and remove items which were deleted from the database.

success: function(data){
    if(data.success){
        //remove deleted items
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Krish I can't help you with php code right now, on the JS side you can use .remove() on your elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.