I have a normal css/html/js modal window and it is a simple contact form. It has a send button that I'd like to turn into a Thank You modal window after the contact form is processed via php. How would I go about doing this?
Try to put this on your code
<?php
if($send)
{
echo"<script type='text/javascript'>";
echo "alert('Thank You.');";
echo "</script>";
}
else{
echo"<script type='text/javascript'>";
echo "alert('Fail.');";
echo "</script>";
}
?>
Related
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>";
if($row['psw']===$passwordphp)
{
echo '<script language="javascript">';
echo 'alert("LOGIN SUCCESSFULL")';
echo '</script>';
header("Location: registration.php");
}
else{
echo '<script language="javascript">';
echo 'alert("CHECK EMAIL OR PASSWORD")';
echo '</script>';
header("Location: login.php");
}
Here is the code. If I don't add header then JavaScript runs but when I add header Javascript stop running only executes the header function. Why is so? How can make both of them work properly?
A Location header tells the browser to drop everything and do something else. You can't do a Location redirect and output HTML.
That said, this code should throw a "Headers already sent" error message, as you can't do a header call after echoing anything to the browser.
As mentioned in the other answer, you can't send a header redirect and also send HTML.
Instead of the header redirect, you can use a Javascript redirect after the alert.
if($row['psw']===$passwordphp) {
echo '<script language="javascript">';
echo 'alert("LOGIN SUCCESSFULL");';
echo 'window.location = "registration.php";';
echo '</script>';
} else {
echo '<script language="javascript">';
echo 'alert("CHECK EMAIL OR PASSWORD");';
echo 'window.location = "login.php"';
echo '</script>';
}
I am updating the records using php/mysql. and after all is done .
I run this code for user confirmation about the activity.
echo '<script language="javascript">';
echo 'alert("We Have credited your account")';
echo '</script>';
How Do i refresh the page so that once user clicks ok, it will refresh all the details again and should display users with new values from the db.
If I use this right after the alert.
header('Location: '.$_SERVER['REQUEST_URI']);
It does not displays the alert message and simply refreshes the page.
Alerts are weird but you can do something like the below as your echo. Alert returns an undefined.
echo "<script language='javascript'>";
echo "if(!alert('We Have credited your account')){
window.location.reload();
}";
echo "</script>";
UPDATE
Right after posting this I realized that the alert will block any other js from running. You can literally just do
echo "<script language='javascript'>";
echo 'alert("We Have credited your account");';
echo 'window.location.reload();';
echo "</script>";
Edit to handle page refresh (submit by OP):
echo "<script language='javascript'>";
echo 'alert("We Have credited your account");';
echo 'window.location.reload();';
echo "</script>";
} else {
echo '<script language="javascript">';
echo 'alert("Please Paste Exact URL Here")';
echo '</script>';
}
window.location.reload and header('Location: '.$_SERVER['REQUEST_URI']) will keep you in a loop "basically" not static "locations", if you die in game you get reloaded to a point or location, you can't replace where you spawn until you got past that point (^.^).
Use window.location.replace("your_page.php") or window.location.href = "your_page.php"; because reload and header is more used for auto redirection to different locations while replace and href is more used to "escape" your current page and load it from scratch.
So do something like this if your query passes:
if ($conn->query($sql) === TRUE) {
echo "<script language='javascript'>";
echo 'alert("Your alert msg");';
echo 'window.location.replace("your_page.php");';
echo "</script>";
}
else{
echo "FAIL";
}
It will prevent data insertion or strings from going in a loop over and over again. I had a pc's USB ports that malfunctioned once and it kept on inserting and alerting in on of my databases it was an EPIC disaster took a while cleaning it up.
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
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;