why wont javascript show the alert? - javascript

beginner here.
I do not understand why no alert pops up when I use the header statement,but the alert pops up just fine when I remove the header location line. Really confused :(
<?php
include "connect.php";
session_start();
// if(isset($_SESSION['user_id'])
if(isset($_POST['register'])){
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
$name=$_POST["name"];
$roll=$_POST["roll"];
$year=$_POST["year"];
$pass=$_POST["register_password"];
$sql="INSERT INTO students (roll_id,name,year,password) VALUES
('$roll','$name','$year','$pass')";
$query=mysqli_query($conn,$sql);
if(!$query)
{
echo "Not Inserted" . mysqli_error($conn);
}
header("location:index.php?registered=true");
}
?>

The problem is header() needs to be sent before ANY output in order to redirect the page to another URL. At that time, you're already echoing things out, so redirects via headers wont work.
In a case such as this, (Where you want to popup a message and then redirect), you should use a javascript redirect:
echo '<script language="javascript">';
echo 'window.location.replace("/index.php?registered=true");';
echo '</script>';
This will output your popup message. After the user hits OK, the javascript redirect code will run, and redirect the page to /index.php?registered=true.
Additionally, you can add the alert box to the page that you're redirecting too. See this example index.php file:
<?php
if (isset($_GET['registered'])) {
echo '<script>alert("You have registered!");</script>';
}
//Continue with rest of page.
If you go this route, don't include ANY output (No echo) on the register page, so that header()s are your only output. This would ideally be a better user experience, as they don't have a white popup page as they're clicking a box that says alert("message successfully sent").

This is because the location header redirects to a different page before the JavaScript alert is shown.
Add the alert to the destination page and it will be shown there.

Echo it like this:
echo "<script>alert('message sent'); window.location.href='url';</script>";

Related

Error Message Display - Form Data PHP Entry in Wordpress

Currently, we have a PHP Form that uses an entry for a redirect to Private sites within our main website, of which code we use the following:
<?php
//Turn the content from text box into variable
$page=$_POST['text'];
if($_POST['text']=='sit') {
//set up a redirect to that page
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://example.com/index.php/private/sit\">";
}
else if($_POST['text']=='pony') {
//set up a redirect to that page
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://example.com/wp-content/uploads/sites/2/client/pony/index.html\">";
}
Originally, we had implemented a final line that if someone inserted something wrong, it would refresh the page:
else {
echo "<meta http-equiv=\"refresh\"content=\"0;URL=http://example.com\">";
}
?>
How to show a message that says "Error" in Bold and Red next to the form?
You can send a parameter in the URL you use to refresh. And then check on the parameter after the URL refresh by Javascript and add the error.
In this case you will echo something like this instead
echo "<meta http-equiv=\"refresh\"content=\"0;URL=http://example.com?error=1\">";
Then in your html, use Javascript or PHP to check on the URL param and render error with style.

Javascript does not redirect correctly

I have a form to upload an image and if it is successful, it should redirect to the same page and should be display the image. I used the below php and javascript code, and although uploading is successful it shows empty page. If I refresh the upload page manually, the image will load.
I used the below code to pass data to upload page. Unable to find any issue.
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
echo '<script type="text/javascript">';
echo 'alert("Successfully Uploaded Image");';
echo "window.location.href ='../htdocs/reg-image.php?nic=$nic' ";
echo 'window.location = "../htdocs/reg-image.php";';
echo '</script>';
}

redirect after alert box

I want to redirect page after clicking an alert box "OK" button
This is my code, but this is not working. The alert box is coming up but when I press OK button it redirects to the same page. I want to redirect http://localhost/project/index.php/Con_Group_View this page.
else
{
echo '<script type="text/javascript">';
echo'alert("Your Group Already Created ");';
echo 'window.Location="http://localhost/project/index.php/Con_Group_View";';
echo '</script>';
}
You want window.location.href = "http://localhost/project/index.php/Con_Group_View" not window.Location. Remember, names are case-sensitive in javascript.
Also note this is a duplicate of How to redirect to another webpage in JavaScript/jQuery?
It is
echo '<script type="text/javascript">';
echo'alert("Your Group Already Created ");';
echo 'window.location = "http://localhost/project/index.php/Con_Group_View";';
echo '</script>';
JavaScript is case-sensitive, there is a difference between window.location and window.Location

show a hidden div after form submit

I save the data using POST method from a form.
After the data has been saved, page reloaded, I want to show a hidden div in the page.
onsubmit="showHide(this); return false;"
shows the specified div but does not save data.
any ideas?
LE:
to make it more complicated: the form that trigges the page reload is on the div that i want to re-show. initialy i make the div visible with:
<a class="articleLink" href="javascript:void(0);" onclick='$("#ModAdd<?php echo $rrows['id_']; ?>").show("slow");'></a>
No data will be sent since you have a return false; in your onSubmit.
If you want the user to stay on the same page, you'll need Ajax.
Else, you have to show your div on the page that receives the data from your form.
You have to display the div after the reload.
onsubmit will display it right away (on the same page). So check if the $_POST is set in php after reloading the site and then display the div
Example
<?php
if (isset($_POST)):
?>
<div>Saved successfully</div>
<?php
endif;
?>
You might try something like this after reloading the page, instead of onsubmit:
$POST = $POST['myPost'];
$Message = '<script type="text/javascript">';
$Message .= 'document.getElementById("' . $IDName . '").style.display="block";'; // Show the hidden DIV
$Message .= 'document.getElementById("' . $IDName . '").innerHTML="' . $POST . '";'; // Output POST value
$Message .= '</script>';
echo $Message;

how to redirect the user to the referer page

I have this part of my script
<?php
else:
?>
<script>
alert("You are not allowed to edit this CV!");
</script>
<?php
echo '<meta http-equiv="refresh" content="1"; url="'.$the_class->settings[0]['DomainName'].'myresume.php"';
endif;
?>
the objective is, after the alert box popped-out and the "ok" button was clicked,
the user should be redirected to http://www.mydomain.com/myresume.php
now the problem is, before the page loads the redirection, the alert box keeps popping out without reaching the destination page at all...how to fix this ?
You can do something like this if you want to always send them back to http://www.mydomain.com/myresume.php:
<script>
alert("You are not allowed to edit this CV!");
window.location.href = 'http://www.mydomain.com/myresume.php';
</script>
You can do something like this if you instead want to send them back to whatever page they were on previously:
<script>
alert("You are not allowed to edit this CV!");
history.back();
</script>
the problem seems to be that you are trying to set a header after sending content.
You can try using the header function. If that does not work, you can simply remove the alert.
If you need to display the alert you can put a window.location in the script tag and remove the php part.
You can also experiment by turning the output buffering off using ob_implicit_flush.
How about sending them back using javascript:
<?php
else:
?>
<script>
alert("You are not allowed to edit this CV!");
document.location =
<?php
echo '"'.$the_class->settings[0]['DomainName'].'myresume.php"';
?>
;
</script>
<?php
endif;
?>

Categories

Resources