Hybreeder Hybreeder - 2 months ago 10
PHP Question

How to pass array records from process page to index page in php?

I am able to display array record in my process page and now i have to pass that records on index page.In array there are more than 10 records.
Once that records will display on index after that i want to display all record on sweet alert popup.

What i am achieving...i am entering firstname in sweet alert and that fname will redirect process page and check that fname is available or not if available and how many users available on same name that all will redirect to index page.
I am able to retrieve all records in process page but i have to redirect all records on index page.

Getting error:Undefined variable: _SESSION and Invalid argument supplied for foreach()


Would you help me in this?

Process.php

$array_record=array();
$array_record['idsa']=$row["Id"];
$array_record['Fname']=$row["First_name"];
$array_record['Lname']=$row["Last_name"];
$_SESSION['arr']=$array_record;
// I tried below echo it display record on process page.


/*
$session=$_SESSION['arr'];
foreach($session as $val)
{
echo $val.'<br>';
}
*/
header('Location: index.php?search=1');


Index

if(!empty($_GET['search'])):

$session=$_SESSION['arr'];
foreach($session as $val)
{
echo $val.'<br>';
}

echo "<script>
setTimeout(function() {
swal({
title: 'You details !',
text: '$val',
type: 'info',
closeOnCancel: true,
html:true

}, function() {
window.location = 'index.php';
});
}, 1000);
</script>";
endif;?>

Answer

In order to get all the array records from process.php to index.php, set that array in SESSION and get the session records in index.php.

index.php

<?php

//Submitting form here => action ="process.php"


?>

process.php

<?php
  //get the records from DB and set the values in session
  // Redirect to index.php

session_start();
$_SESSION['arr'] = $row; // $row is the array from Database
header("Location:index.php");
exit;
?>

index.php

<?php
//Use the values in session 
//Unset after using 

session_start();
if($_SESSION['arr']){

$session=$_SESSION['arr']; 
foreach($session as $val) 
{ 
echo "<pre>"; 
print_r($val); 
echo "</pre>"; 
}
//you can display the array using print_r($_SESSION).
}
?>
Comments