I cant understand this code of php $_GET - php

Please explain this code. What does "status" and the rest of the code mean?
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks"
<?php // if status=thanks in the query string, display an thank you message instead of the form ?>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly!</p>
<?php } else {

Status is an variable from url which is checked for its value is set and value equals to "thanks".

The below code embedded the html and php codes,
The use of below code is you can access both html tags and php values in same file.
<?php
// if status=thanks in the query string, display an thank you message instead of the form
?>
the above code, provide the php comment block, it give suggestion to developer.
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
the above code used to get the value from url (using $_GET variable) (for example www.example.com/index.php?status=thanks)
if the status is set in url and the value is 'thanks' means, html tag will run otherwise run the else part.
<p>Thanks for the email! I’ll be in touch shortly!</p>
else part,
<?php } else {
// do somethink..!!
}

if you r use $_GET method in php first a follow Your URL : https://www.example.in/webhp?status=thanks.check get variable status store value of thanks.After u check in php GET method. like the following code.
<?php
if($_GET['status'] == 'thanks'){?>
<p>Thanks for the email! I’ll be in touch shortly!</p>
<?php }else{?>
<p>Error</p>
<?php }?>

You need konw that:
#See-1 PHP Predefined Variables
#see-2 $_GET is An associative array of variables passed to the current script via the URL parameters.

For example:
you add ?status=thanks
like
http://yourdomain.com/index.php?status=thanks
it will show the <p>Thanks for the email! I’ll be in touch shortly!</p>
if there is no status or the status is not thanks it will not show that comment.
Example:
create file named test.php put it in htdocs localhost
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") : ?>
<p>Thanks for the email! I’ll be in touch shortly!</p>
<?php endif; ?>
try to go to your http://localhost/test.php and http://localhost/test.php?status=thanks and http://localhost/test.php?status=you
check what will be happened?

Related

Five unique pages lead to one page. Can I change the <h1> according to the page they came from?

I have five unique forms each on a page of HTML. They then go to a PHP file to send the e-mail of data. Next they go to the HTML thank you page. I am hoping to change the heading according to which form they just submitted.
For example, if they submit a review, the should read "Thank you for your review" etc.
Technically all of these are saved as php files but only the e-mail page has php items.
Like <?php echo("<p>". $mail->getMessage()."</p>"); ?>
You should redirect to another php file and pass a parameter on url. Example:
sendemail.php
<?php
/** After send the email, check what kind form is (I don't know how do you check this).
This example is just to show you: */
if ($formType == 'review') {
$type = 'review';
} else if ($formType == 'anothertype') {
$type = 'anothertype';
}
header('Location: /thankspage.php?type=' . $type);
?>
thankspage.php
<?php
$type = $_GET['type'];
if ($type == 'review') {
echo '<h1>Thanks for your review</h1>';
} else if($type == 'anothertype') {
echo '<h1>Thanks for your anothertype</h1>';
}
?>
One way put a hidden field in your forms that'll get passed with the other form data. Then put an if statement on the thank you page and echo the appropriate message.
However, that'll only work either if you change the thank you page to php or change the page that receives and processes the form data to echo the thank you message as well

PHP syntax - shows text and not link

With this snippet of code, I'm attempting to show a clickable link (if "admin" is logged in), which will redirect me to adminarea.php
Right now it just prints out "Admin" in text. Nothing to click on. Just simple text.
Am I missing anything? Surely I got it wrong but I cannot see what's missing.
Here is the code:
<?php if (getUser("user") == "admin") { ?>
<option value="adminarea.php">Admin</option>
<?php } ?>
You're printing an option, which is part of the select form input. You're probably looking for an anchor?
Admin
Possibly a better way to do this would be to declare two options for a variable in your PHP first. Something like:
<?php
if(getUser("user") == "admin") {
$adminlink = 'Admin';
} else {
$adminlink = NULL;
}
?>
And in the html:
<?php echo $adminlink; ?>
This would show the href link if the PHP condition was true, and would display nothing if not. Hope this helps!
Well based on your title am assuming you want a link. By the way you can use PHP friend html syntax instead of making the code look "dirty".
<?php if(getUser("user") == "admin"): ?>
Admin
<?php endif; ?>

Get Parameter from URL using PHP

I have a url:
domain.com/?city=newyork&postback=test
I am currently successfully passing the postback parameter using the PHP below. However, I can not figure out how to also pass the city parameter.
<?php session_start();
if(isset($_SESSION['postback'])) {
if($_GET['postback'] == "") {
header ("Location: qualify-step2.php?postback=".$_SESSION['postback']);
}
}
?>
Can someone please help me edit the code successfully? Any suggestions are greatly appreciated.
You should use the $_GET superglobal to get params from url... In your case $_GET['city'] holds the value newyork and $_GET['postback'] holds the value test.
You mean like this
<?php session_start();
if(isset($_SESSION['postback'])) {
if($_GET['postback'] == "") {
header ("Location: qualify-step2.php?postback=".$_SESSION['postback']."&city=".$_SESSION['city']);
}
}
?>
For geting parameter from URL, you should use $_GET['param'];.
Note: In here, You should use Get method for sending data.

Best Practice for PHP exit()

I am working on a PHP project and I have a file called login.php. This file contains a HTML code that basically consists of a form that has 2 inputs (used for email and password) and a submit button. Before the HTML there is a PHP script that checks if the inputs are valid (not empty, the user exists in the db). I use an hidden input field (called formsubmitted) to check if the user has pressed the submit button. If the values are valid then the PHP script redirects the user to the home page, if not, the PHP script prints a HTML div in which I store the error message and then the HTML for the form.
I would like to print only the error div, not the rest of the HTML, and for this I could use the exit() function. Now I don't know if this is a good practice, I have searched the web but didn't find any useful. I would appreciate if someone could tell me if it is ok to do so.
Thanks!
Here is how my code looks briefly:
<?php
if (isset($_POST['formsubmitted'])) {
//check if the email and password are valid
if (valid) {
header("Location:home.php");
} else {
echo "<div id='error-section'>";
echo "message";
echo "</div>"
}
}
exit();
?>
<!DOCTYPE html>
<html>
...............
</html>
To answer your question: I personally wouldn't use exit() statements anywhere in your code (unless it's after a REST API echo(json_encode(array()) echo statement.) It just can cause a lot of head aches down the road with debugging errors.
(It also can break \ob_*()\ calls as well as the majority of framework's display modules - because the framework won't be able to get to it's echo statements.)
My Practice:
I always have a .errors div in my forms:
<?php if (isset($errors) && !empty($errors): ?>
<?php foreach ($errors as $error): ?>
<div class="alert alert-danger" role="alert">
<strong>Error: </strong> <?php echo $error['message'] ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<form>
<!--- form here -->
</form>
This way you just pass the $errors array into your form and everything works honkey dory and you can avoid using exit() - using exit() has it's time and place, but because it doesn't give you a message or log anything it can be a royal pain to debug later on.
As a closing note, this method allows you to pass an array to the errors like so:
array(
'first_name' => 'First Name Required',
'last_name' => 'Last Name Required',
'zip_code' => 'Must enter a valid zip code'
)
Which allows you to, when you're rendering your form add an .error class to your input fields which will help your users locate which input had the problem - always nice:
<input id="first_name" type="text" class="my_input_class<?php (isset($errors) && isset($errors['first_name'])) ? ' error' : '' ?>">
try this :(UPDATED)
<?php
if (isset($_POST['formsubmitted'])) {
//check if the email and password are valid
if (valid) {
header("Location:home.php");
} else {
die("<html>
<head>
<!-- ADD STUFF HERE IF YOU WANT-->
</head>
<body>
<div id='error-section'>message</div>
</body>
</html>");
//you can use exit if you want too
}
}
?>
<!DOCTYPE html>
<html>
...............
</html>
The best practice is to show the form with an error message so the user has opportunity to try again. Not valid credentials can have several meaning. In any case you shouldn't just show error message and dead end for the user. Provide a meaningful error message with opportunity to try again.

use php to change a html elements inner text

I have a basic form, which i need to put some validation into, I have a span area and I want on pressing of the submit button, for a predefined message to show in that box if a field is empty.
Something like
if ($mytextfield = null) {
//My custom error text to appear in the spcificed #logggingerror field
}
I know i can do this with jquery (document.getElementbyId('#errorlogging').innerHTML = "Text Here"), but how can I do this with PHP?
Bit of a new thing for me with php, any help greatly appreciated :)
Thanks
You could do it it a couple of ways. You can create a $error variable. Make it so that the $error is always created (even if everything checks out OK) but it needs to be empty if there is no error, or else the value must be the error.
Do it like this:
<?php
if(isset($_POST['submit'])){
if(empty($_POST['somevar'])){
$error = "Somevar was empty!";
}
}
?>
<h2>FORM</h2>
<form method="post">
<input type="text" name="somevar" />
<?php
if(isset($error) && !empty($error)){
?>
<span class="error"><?= $error; ?></span>
<?php
}
?>
</form>
If you want change it dynamically in client-side, there is no way but ajax. PHP works at server-side and you have to use post/get requests.
Form fields sent to php in a $_REQUEST, $_GET or $_POST variables...
For validate the field param you may write like this:
if(strlen($_REQUEST['username']) < 6){
echo 'false';
}
else{
echo 'true';
}
You can't do anything client-side with PHP. You need Javascript for that. If you really need PHP (for instance to do a check to the database or something), you can use Javascript to do an Ajax call, and put the return value inside a div on the page.

Categories