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

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

Related

alert and redirect in codeigniter php using javascript not working

i have a function my controller, when the user clicks a button, the function runs and should show the alret box and then redirect the user to the same page, so i did the following code:
public function addtowishlist()
{
if($this->session->userdata('id'))
{
$id =$this->uri->segment(3);
$this->product->addtowishlist($id);
echo '<script type="text/javascript">alert("' . $pname . '")</script>';
redirect($_SERVER['HTTP_REFERER']);
}
}
however the issue is the alert is coming fine but the redirect to same page is not happening, only i get a blank page, can anyone please help me with this, thanks in advance
Your redirect will execute before alert since PHP is serverside and Javascript is client side, so what you need is redirect with javascript like this
public function addtowishlist() {
if($this->session->userdata('id')) {
$id =$this->uri->segment(3);
$this->product->addtowishlist($id);
echo '<script type="text/javascript">
alert("' . $pname . '");
window.location.href = "'.$_SERVER['HTTP_REFERER'].'"; ;
</script>';
}
}

including html tag in a php code, specifically dealing with href function inside a php block

enter the html code once a condition is satisfied but then i aim to use php values and html tags together.
tried tag etc and got most of the part in running just unable to deal with href beacuse it is referencing to some values.
not sure where to use "" or ``.
<?php.....
echo `Yes`;
?>
"yes" should work as a hyperlink but at the moment the php values are not processed that`s why no result.
You are using wrong sequence of quote
<?php
...
?>
<?php
echo '<a href="limitdatabase.php?Dropdown=' .
$_GET['Dropdown'].
';&search='.
$search_name .
';&wise='.
$_GET['wise'].
';">Yes</a>';
?>
and you should use single quote and not backtics for string
You are using incorrect concatenation. Whenever you want to add any PHP variable then you need to close the braces and need to start after PHP variable as below:
<?php
echo 'Yes';
?>
Also you should wrap Dropdown and wise in braces as it will not work directly in get as you have used. Hope it helps you!
You can use combination of html with php inside:
<a href="limitdatabase.php?Dropdown=<?PHP echo $_GET[Dropdown] . "&search="
. $search_name . "&wise=" . $_GET[wise]; ?>">Yes</a>
Also, you can input whole link in string:
<?php
$mylink = "limitdatabase.php?Dropdown=" . $_GET[Dropdown] . "&search=" . $search_name . "&wise=" . $_GET[wise];
?>
YES

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

How to Refresh a .php Page After Form Submitted?

This seems to be pretty basic but I've looked at all the suggestions (online) I can find -- and they did not work -- so I thought I might as well ask the Stack-Community.
I simply want my page to refresh AFTER the form is submitted.
I have my php Session Header first on the .php page:
if(isset($_GET['logout'])) {
$_SESSION['username'] = '';
header('Location: ' . $_SERVER['PHP_SELF']);
}
if(isset($_POST['username'])) {
if($userinfo[$_POST['username']] == $_POST['password']) {
$_SESSION['username'] = $_POST['username'];
}else {
echo "Wrong username or password.";
}
}
I then have a form start code:
<form action="" method="post" >
I have used different inside the action " " but these don't seem to work.
I also then have a input type " " that looks like this:
<input type="submit" name="submit_invoice" value="Submit" />
However, nothing I alter in this makes the page load AFTER the form is submitted.
Here are some of the things I've tried on either the FORM or the INPUT TYPE areas OR after the query to update the mysql record in the DB. Please Note: I am able to update the data in the DB so the code is working . . . but nothing seems to work. I must be doing a little thing wrong.
header('Location: ' . $_SERVER['PHP_SELF']);
location.reload();
When I use header() the error is that the header is already set up so can't do it again. Location reload simply does not reload.
Any help, much appreciated.
Try this:
header('Location: ' . $_SERVER['PHP_SELF']);
die('<META http-equiv="refresh" content="0;URL=' . $_SERVER['PHP_SELF'] . '">');

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;

Categories

Resources