When I include the # glyph in my statement, to return to an anchor on the page, the PHP code fails.
I've tried everything I can think of to resolve this issue, escaping PHP characters, writing JavaScript functions and the list goes on.
if (isset($_POST['name'])) {
$_POST = array();
echo "<script>window.location.href='Contact_Us.php#myForm'</script>";
} else {
unset($_POST);
}
There are no error messages.
The page appears to refresh and the code to unset the POST variables fails.
If you really want to do things that way simply drop out of PHP instead of trying to echo things.
if (isset($_POST['name'])) {
$_POST = array();
?>
<script>window.location.href='Contact_Us.php#myForm'</script>
<?php
} else {
unset($_POST);
}
Related
I wrote a simple php file to open different websites using different URLs.
PHP code is here.(it's file name is user.php)
<?php
$id = $_GET["name"] ;
if ($id=joe) {
header('Location: http://1.com');
}
if ($id=marry) {
header('Location: http://2.com');
}
if ($id=katty) {
header('Location: http://3.com');
}
?>
I used those 3 methods to call php file.
1.http://xxxxxx.com/user.php?name=joe
2.http://xxxxxx.com/user.php?name=marry
3.http://xxxxxx.com/user.php?name=katty
But php file opens only http://3.com at every time.How to fix this.?
how to open different websites for each names.?
Your comparison is wrong. joe, marry and katty are string type
<?php
$id = $_GET["name"] ;
if ($id=='joe') { //<--- here
header('Location: http://1.com');
}
if ($id=='marry') { //<--- here
header('Location: http://2.com');
}
if ($id=='katty') { //<--- here
header('Location: http://3.com');
}
?>
Here is PHP comparison operator description.
http://php.net/manual/en/language.operators.comparison.php
First off, using the == vs = is what's wrong with what you have, however whenever you are doing a script take care to not be redundant. You may also want to think about making a default setting should no conditions be met:
<?php
# Have your values stored in a list, makes if/else unnecessary
$array = array(
'joe'=>1,
'marry'=>2,
'katty'=>3,
'default'=>1
);
# Make sure to check that something is set first
$id = (!empty($_GET['name']))? trim($_GET['name']) : 'default';
# Set the domain
$redirect = (isset($array[$id]))? $array[$id] : $array['default'];
# Rediret
header("Location: http://{$redirect}.com");
# Stop the execution
exit;
So it looks like your question has been answered above but it's probably not that clear for you, if you're just beginning (using arrays, short php if statements etc).
I'm assuming that you're just learning PHP considering what you're trying to achieve, so here is a simplified answer that is easier to understand than what some other people have posted here:
<?php
// Check that you actually have a 'name' being submitted that you can assign
if (!empty($_GET['name'])) {
$id = $_GET['name'];
}
// If there isn't a 'name' being submitted, handle that
else {
// return an error or don't redirect at all
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
// Else your code will keep running if an $id is set
if ($id == 'joe') {
header('Location: http://1.com');
}
if ($id=marry) {
header('Location: http://2.com');
}
if ($id=katty) {
header('Location: http://3.com');
}
?>
Hope this helps you better understand what's happening.
You should use == for conditional statements not =
if you use = , you say :
$id='joe';
$id='marry';
$id='katty';
if($id='katty') return 1 boolean
I want to redirect a php page using php if statement. I did the redirect code with javascript but its not working.
Can someone please help me modify my code if i missed something out or help me out with a better solution.
Below is the code;
$vbi = $row_rsRek['duck'];
if ($vbi == "blocked"){'<script>window.location.href = "http://www.url.com/login.php";
</script>
';}
else {echo "NOT WORKING";}
I tried this too
$vbi = $row_rsRek['duck'];
if ($vbi == "blocked"){header("Location: www.url.com/login.php");}
else {echo "NOT WORKING";}
PHP has a build in feature for your needs
header("Location: path/to/file");
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
http://php.net/manual/en/function.header.php
You can use header function :
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
<?php
$vbi = 'blocked';
if ($vbi == "blocked")
{
header("Location: http://www.yourwebsite.com/user.php");
}
else
{
echo "NOT WORKING";
}
?>
see this link for more information and discuss :
How to make a redirect in PHP?
i'm trying to refresh page every 3 second, the url page change with $_GET variable.
i'm trying to save $_GET var into session and cookie, but get error header has already sent.
how to change url after page reload ?
here my script :
Index.php
<?php
session_start();
$skill =$_SESSION['skill'];
?>
<script type="text/javascript">
var auto_refresh = setInterval(function () {
$('#src2').load('monitor.php?skill=<?php echo $skill;?>').fadeIn("slow");
}, 3000);
</script>
monitor.php
<?php
include "conn.php";
session_start();
$_SESSION['skill'] = $_GET['skill'];
if ($_SESSION['skill']=='')
{
$a ="bro";
$_SESSION['skill']=4;}
elseif ($_SESSION['skill']==4){
$a = "yo";
$_SESSION['skill']='5';
}
elseif ($_SESSION['skill']==5){
$a = "soo";
}
?>
First off, "headers already sent" means that whichever file is triggering that error (read the rest of the error message) has some output. The most common culprit is a space at the start of the file, before the <?php tag, but check for echo and other output keywords. Headers (including setting cookies) must be sent before any output.
From here on, this answer covers how you can implement the "refresh the page" part of the question. The code you provided doesn't really show how you do it right now, so this is all just how I'd recommend going about it.
Secondly, for refreshing the page, you will need to echo something at the end of monitor.php which your JS checks for. The easy way is to just echo a JS refresh:
echo '<script>window.location.reload();</script>';
but it's better to output some JSON which your index.php then checks for:
// monitor.php
echo json_encode(array('reload' => true));
// index.php
$('#src2').load('monitor.php?skill=<?php echo $skill;?>', function(response) {
if (response.reload) window.location.reload();
}).fadeIn('slow');
One last note: you may find that response is just plain text inside the JS callback function - you may need to do this:
// index.php
$('#src2').load('monitor.php?skill=<?php echo $skill;?>', function(response) {
response = $.parseJSON( response ); // convert response to a JS object
if (response.reload) window.location.reload();
}).fadeIn('slow');
try putting
ob_start()
before
session_start()
on each page. This will solve your problem.
Without looking at the code where you are setting the session, I do think your problem is there. You need to start the session before sending any data out to the browser.
Take a look at: http://php.net/session_start
EDIT:
Sorry, a bit quick, could it be that you send some data to the browser in the 'conn.php' file? Like a new line at the end of the file?
I couldn't get the code below to display the success word, any idea what's the problem with the code below?
Thanks in advance.
<script type="text/javascript">
function verification(){
var s = document.test_form.textfield.value;
if (s == "") {
alert("Please enter a value");
return false;
} else {
<?php echo "sucess"; ?>
}
}
</script>
You need to wrap your echo in an alert
alert("<?php echo 'sucess'; ?>")
Your output in javascript is:
else {
sucess
}
What this mean? Try something like this if you want to force output by PHP:
else {
<?php echo "alert('success');"; ?>
}
Or just return true to confirm submitting the form:
else {
return true;
}
You can do 2 things:
console.log("<?php echo 'success'; ?>"); // Will display a message into JS console
or:
alert("<?php echo 'success'; ?>"); // Will display a message into JS alert box
In your code, you only writing 'success' inside the javascript code, and when the browser will try to execute this JS code, it will not understand 'success'and it will throw an error.
If you want to display the text on your page, you can use inside a document.write:
document.write("<?php echo "success"; ?>");
Otherwise, you can use an alert() or console.log()
document.getElementById('body').innerHTML('<?php echo 'sucess'; ?>');
assuming that your body tag has the id body(<body id="body">)
EDIT. of course you can do that with every tag that has an id.
If you are getting PHP to write Javascript you might want to think about using a templating engine like Smarty or Twig.
For the case the file is a Javascript
If the original file is an external .js (not a .php) and is being read on the client side (browser) then it simply does not know how to parse PHP.
Therefore when the browser gets to:
} else {
<?php echo "sucess"; ?>
}
It simply does not recognize the <?php and thus is throwing an error.
Replace the PHP string by alert("success") or, if you do want to query the server and have the server output something take a look into Ajax.
For the case the file is a PHP and the Javascript code is embedded in the html
Replace the code by
} else {
<?php echo 'alert("sucess";)' ?>
}
I am writing PHP to validate form data. I am trouble shooting my alert boxes.
This is the bones of my code:
function emailcheck ($email1)
{
$regexp="/^[a-zA-Z0-9_.]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]+$/";
if (preg_match($regexp,$email1))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($_POST['submit'])) {
if ($emailcheck == TRUE){
//send email
//various fields etc
echo '<script type="text/javascript"> alert("Your form has been submitted.") </script>';
}
else
{
echo '<script type="text/javascript"> alert("Enter a valid email address.") </script>';
return FALSE;
}
}
When I test the form for the emailcheck function to be FALSE I get this;
followed by this:
How can I fix this? Thanks
edit
I am not concerned with the entire php/javascript validation; I am looking at a specific detail- what am I doing wrong with my functions here? Why is it printing off the alert.. there must be something basic I've overlooked.
There is, obviously, a problem with how I am using the isset function, and the way I am calling in the emailcheck function.
I have client side validation, but am focused on the php validation in this question - so please, no need to discuss javascript validation.
You've got some kind of syntax/ quoting error. Backup your file, then binary search A/B by removing or drastically simplifying sections of code, until the symptom is fixed.
You have then found your problem, & can fix it in the restored file.
Postscript: error may be in the PHP quoting or <? syntax -- we're seeing Javascript either being emitted, or parsed by the browser, as HTML.
Try viewing the source & checking what the browser sees?
PS2: We're also seeing PHP code (the } else {) being shown in the browser. Your code sample doesn't show the <? PHP start -- you've got some kind of error in the <? ?> PHP syntax.
By the way, I'm the only person who is actually picking up these obvious symptoms & helping you.
$emailcheck is a variable and not a function. it should be
$emailcheck = emailcheck($email);
if($emailcheck === TRUE): /* more code */ endif;
IMO I would use the function that goes with the PHP installation.
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<script>alert('GOOD email');</script>";
} else {
echo "<script>alert('BAD email');</script>";
}
link to php fiddle
I think you need to change your test in your if statement.
function emailcheck ($email1) {
$regexp="/^[a-zA-A-Z0-9_\.]+#[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/";
if (preg_match($regexp,$email1)) {
return TRUE;
} else {
return FALSE;
}
}
if (isset($_POST['submit'])) {
$emailAddy = "someone#somewhere.com"; //get email address from form here
if (emailCheck($emailAddy)){
//send email
//various fields etc
echo '<script type="text/javascript"> alert("Your form has been submitted.");</script>';
} else {
echo '<script type="text/javascript"> alert("Enter a valid email address."); </script>';
}
}
Your current statement isn't checking the return value of your function, it is testing the value of the variable $emailCheck, which isn't actually set to anything.
Edit
I also just noticed in your regexp string you have a capital 'S' instead of a $, assuming that you wanted to signify the end of the string. Also, both of your alert() calls were missing the semicolon at the end. I've tried out this code on my end and it seems to work. I've modified my answer to reflect the changes. Hope that helps!