show a hidden div after form submit - javascript

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;

Related

How to display $name Input that a user submitted in a form within a $alert when user pushes the submit button?

I am stuck and hoping someone can help.
I am trying to get the user's input (their $name) to show in the $alert'' when user submit form. Example: User types in to the form the following: ** Name: Joe , Tel: XXX.XXX.XXXX , Email: Joe#example.com *** and pushes submit.
-- page refreshes and says. "Message sent Joe, thank you for contacting us!"
$mail->send();
$alert = <div class="alert-success">'
<span>Message sent $name, thank you for contacting us!</span>
</div>';
You need to fix your typos, like this:
$mail->send();
$alert = '<div class="alert-success">'.
'<span>Message sent '.$name.', thank you for contacting us!</span>'.
.'</div>';
Now that you have a template in $alert you will need to make sure that you display it. The simplest way to do so is to echo it, like
echo $alert;
But you may need to display it somewhere else than the current position. Also, PHP is a template language as well, so in some environments you can do something like
$mail->send(); ?>
<div class="alert-success">
<span>Message sent <?php echo $name; ?>, thank you for contacting us!</span>
</div>
<?php

why wont javascript show the alert?

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>";

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

display a div/span only after $_SERVER['HTTP_REFERER'] has been executed

I tried to approach it numerous ways, but somehow I'm not able to figure it out. Maybe you guys can help me?
I need to display a certain div/span or whatever container to display a text after page goes back via
header('Location: ' . $_SERVER['HTTP_REFERER']);
The reason why I need such a wierd approach to display something is that I'm using a href/GET combination to run a PHP function when my link gets klicked. (submit button is in use by another function/module so I can't use that )
HTML part
<p id="show_project">
<a id="add_to_project" name="add_to_project" href="?function">
Add to project
</a>
</p>
PHP function part
$this->getProductinfo();
if (isset($_GET['function'])){
$this->getSQLQuery($_GET['function']);
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
Any ideas ?
BR's
Why not add a message to your header() call
$this->getProductinfo();
if (isset($_GET['function'])){
$this->getSQLQuery($_GET['function']);
header('Location: ' . $_SERVER['HTTP_REFERER'].'?msg=We got a message');
}
Get the message
<?php
session_start();
if (!empty($_GET['msg'])){
$_SESSION['msg'] = $_GET['msg'];
header('Location: ' . $_SERVER['HTTP_REFERER']);
die;
}
else{
if (!empty($_SESSION['msg'])){
$msg = $_SESSION['msg'];
unset($_SESSION['msg']);
}
}
?>
<?php
/* Later in the document */
echo '<div>' . $msg . '</div>';
?>
You will need to sanitise and validate the $_GET variable
borrowed from here

Updating session variable using onclick in php

I am trying to update the $_SESSION['state'] whenever the user clicks on the anchor tag by firing the onclick event. The variable $state_names_array as all the names of the states of a specific country. But problem is that no matter whichever of the anchor tag I click on, the $_SESSION['state'] is always getting updated by the last element of the $state_names_array.
<?php
foreach($state_names_array as $value){
$temp = '<?php $_SESSION["state"]=$value;?>';
?>
<?php echo $value ?><br>
<?php
}
?>
I will not code whole code, I will just show you an example, how to do it:
<?php
foreach ($state_names_array as $value) {
echo ''.$value.'<br>';
}
?>
Then you will have javascript function change_value:
here you will send ajax call to some PHP file, where you will process $_SESSION["state"]=$value;
Also you didnt say, what you want after anchor is clicked, because you will be able to access new $_SESSION["state"] only after refreshing your site...
Also maybe you just want to redirect to index.php, after clicking on some anchor, and that will be enough for you, then just use value as $_GET parameter, f.e.:
<?php
foreach ($state_names_array as $value) {
echo ''.$value.'<br>';
}
?>
and add to you index.php file this lines:
if (isset($_GET['new_value'])) {
$_SESSION["state"] = $_GET['new_value'];
}

Categories

Resources