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>";
}
Related
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>
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
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();
}
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I am facing issue regarding passing javascript variables to php within same function. My code looks like this
else if(msg_type[i] == 'code' ){
var code_action = 'test';
<?php
function foobar_func(){
return "<script>document.writeln(action[i]);</script>";
}
add_shortcode( 'foobar', 'foobar_func' );
?>
}
what i am doing is passing that code_action variable of javascript and returning it to function without using ajax or jquery...is there any possible method to do so...??
any possibilities will be appreciated. Thank you
When you make a request to invoke this page, your <?PHP ?> block is executed in the server. It creates a processed output in the web page (in your code) and send it to the browser. Now the browser executes your <script> </script> blocks.
I hope you understand that passing variables from <script></script> to <?PHP ?> is impossible since the latter happened in the past.
But you can use it other way around by trying to pass variables from the PHP to JS.
When you write
<?PHP
$fromServer = "from php";
echo "<script> var fromServer = " . $fromServer . " </script>
?>
.. it passes data from your PHP to JS.
You can, however, use HTTP POST or GET method to pass data from JS to PHP to be used in the next session.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
i would like to do a script something like this:
<script>
function termektorol(kod)
{
<?php $parancs="delete from mex_telefon where id='?>kod<?php'";
if(mysql_query($parancs))
{
?> alert (kod); <?php
} ?>}
</script>
If i replace the 'kod' with just an actual id it deletes the thing,but i cant get it to work with the 'kod' from the function.I think its only synthax error,but i dont know how to fix it.
A possible solution is to reload page and add this parametre to the request
window.location = ...var='+kod
and in php
<?php
$parancs="delete from mex_telefon where id='.$_REQUEST["var"].'";
?>
Other solution is to use AJAX : send request from Javascript to PHP