how to load the home page after done some function [duplicate] - javascript

This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 7 years ago.
I have to reload automatically to my home after done following coding
if($sql)
{
echo "Updated successfully";
}else {
echo "Con not update" . mysql_error();
}
this is process.php page here I am writing some coding after executing my query I need to redirect to my home.php page with pop up message if $sql updated successfully message(successfully) if not error message.

Also, in pure PHP, without writing a piece of JavaScript code, you can do this:
Header('Location: home.php');
This code is plenty functional on PHP an it redirects you to the requested page.

You need to use JavaScript for both alert and redirection.
if($sql) {
echo "<script>alert('Updated successfully');
window.location.href='home.php';
</script>";
}
else {
echo "Con not update" . mysql_error();
}

Related

why php executes header statement without executing the echo statement [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
$count = mysqli_num_rows($result);
if($count == 1){
echo "<h1><center> Login successful </center></h1>";
} else{
echo "<script>alert('login failed! invalid username or password');</script>";
header("Location: index.php") ;
}
I want to display the alert message as login failed and if the user press ok then it should again go back to the login page.I tried above code but ,that doesn't work.
the browser moves to the login page without showing the alert message.
Is there any alternative ways for this?
echo is working well, but the code is sync in php right there. That is why header runing with echo, but header redirecting you to index.php and echo was seen for a while.
You can store your message to a SESSION and show it on index.php if the msg is exists in SESSION:
.
.
else{
session_start();
$_SESSION['msg'] = 'Login failed! invalid username or password';
header("Location: index.php");
index.php:
session_start();
if(isset($_SESSION['msg'])){
echo $_SESSION['msg'];
unset($_SESSION['msg'];
}

sending alert message before executing the function [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
i have following code:
if(isset($_POST['Button']) && (!empty($_POST['Button']))){
echo '<script language="JavaScript">alert("ALERT MESSAGE");</script>';
echo $classFunctions->Function($db, $_POST);
echo '<meta http-equiv="refresh" content="0;URL=\'refertarget\'">';
}
but the alert message does not appear before the function is executed. Any suggestions how i can handle that alert message appears before the function will be executed?
thanks and regards
This cannot be done in PHP itself. Since PHP is executed on the server, builds the page and then sends it to the client where the JavaScript is executed.
To achieve something close to what you're trying to do would require you to send another request to the PHP file when the alert is closed, which does then execute the function:
<script>
alert('ALERT');
fetch('url/to/php-file');
</script>
remember that there is also confirm instead of alert where you can select Yes or No and only proceed on Yes:
<script>
if (confirm('PLEASE CONFIRM')) {
fetch('url/to/php-file');
}
</script>

Use Popup Javascript in PHP [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I have this code:
<html>
<body>
<script>
var x;
if (confirm("Press a button!") == true) {
x = "You pressed OK!";
<?php $kk="ok"; ?>
} else {
x = "You pressed Cancel!";
<?php $kk="not ok"; ?>
}
document.getElementById("demo").innerHTML = x;
</script>
<?php
echo $kk;
?>
</body>
</html>
When I echo $kk, I obtain always not ok
But I want to print OK or NOT OK. Any helps please?
What you are asking for is absolutely impossible as is, because all PHP code is completely executed before the html is sent to the browser, where the javascript is executed in its turn, hence after.
If you need the result on the http server, you must describe a form in the html page and create a server PHP script at the URL of the form action, then the PHP can get the form elements.
If you need the result in the browser (eg. to change some elements in the page displayed), you must code in javascript.
If you want to exchange between PHP and javascript while staying in the same page, you can use Ajax, or you can use directly the javascript XMLHttpRequest object which can call a server script from the browser javascript. See some examples at w3schools.com

Unexpected response while using php [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I'm developing irctc similar website similar project as college assignment. I'm providing user to search any train running on given date and validating from database. I'm using php and mysql and getting no error as well as invalid response from code. Please look at the code:
if (isset($_POST['btn_no'])) {
$current_date=date("Y-m-d");
$selected_date=$_POST['date'];
$no=$_POST['train_no'];
if ($selected_date>=$current_date) {
$query="SELECT train_no FROM `trains` WHERE train_no='$no'";
$found=$db->query($query);
$rows=$found->num_rows;
if ($rows==1) {
$_SESSION['train_id']=$no;
$_SESSION['date']=$_POST['date'];
header("Location: showtrain.php");
}
elseif ($rows==0) {
?>
<script>alert("Train no. invalid !")</script>
<?php
}
}
elseif ($selected_date<$current_date) {
?>
<script>alert("Wrong date");</script>
<?php
}
}
Every time <script> is executed.
From what I can tell you have an error in you query
$query="SELECT train_no FROM `trains` WHERE train_no='$no'";
WHERE train_no='$no'" will be looking for the string '$no' instead of the variable $no.
Consider trying something like to make use of the value of the variable instead of having MYSQL interprate $no as a string.
$query="SELECT train_no FROM `trains` WHERE train_no=$no";
or
$query="SELECT train_no FROM `trains` WHERE train_no=" . $no;

how to combine javascript alert and header in php? [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
I want to display the javascript alert and the redirect using php. The logical error is that it won't display the javascript alert.
here's my code:
if($sql_update==true){
echo "<script type='text/javascript'>alert('Updated');</script>";
header("location:?tag=student_entry&opr=upd&rs_id=".$_POST['stud_id_txt']."");
}
if your doing embedding js inside php for alert then also use JS redirect inside it
$redirectURL="Your URL";
print("<script>");
print("alert('Updated');");
print("var t =setTimeout(\"window.location='".$redirectURL."';\", 3000);");
print("</script>");
You can make an javascript function to help you alert the user and redirect like this:
function alertAndRedirect(studId) {
alert('Updated');
window.location = "?tag=student_entry&opr=upd&rs_id="+studId;
}
and in php you call it like this:
if($sql_update==true){
echo "<script type='text/javascript'>alertAndRedirect(".$_POST['stud_id_txt'].");</script>";
}
You can not provide output before header ,even if you set output buffer on , you will not be able to alert . So do redirection through js.
if($sql_update==true){
echo "<script type='text/javascript'>alert('Updated');location.href=?tag=student_entry&opr=upd&rs_id=".$_POST['stud_id_txt']."script>";
}

Categories