Just curious to know why the code below gives "unexpected T_ELSE" syntax error:
<?php if (isset($_SESSION["user_id"])) { ?>
<h2>Welcome, <?php echo $_SESSION["user_id"]; ?></h2>
<?php } ?>
<?php else { ?>
<form action="" method="post">
<label for="user">User ID</label>
<input type="text" id="user" />
<label for="password">Password</label>
<input type="password" id="password" />
<input type="submit" value="Login" />
</form>
<?php } ?>
While I keep the } else { on same line, it works fine. I mean the code below just works fine:
<?php if (isset($_SESSION["user_id"])) { ?>
<h2>Welcome, <?php echo $_SESSION["user_id"]; ?></h2>
<?php } else { ?>
<form action="" method="post">
<label for="user">User ID</label>
<input type="text" id="user" />
<label for="password">Password</label>
<input type="password" id="password" />
<input type="submit" value="Login" />
</form>
<?php } ?>
thinking a bit about this, I've come to the realization that this has to be the intended behavior.
consider the following (syntactical wrong ) example:
<?php if ($condition == true) { ?>
<div id="first">Yey</div>
<?php } ?>
<span id="second?">where am I?</span>
<?php else { ?>
<div id="first">Ney</div>
<?php } ?>
the span element would be in an undefined state
It seems to me, that you can't start a new code block with an else statement without a preceding if.
You could…
A) write your code in one block, e.g.
<?php }
else { ?>
B) or use the alternative syntax, if you are working with multiple code-blocks:
<?php if (isset($_SESSION["user_id"])): ?>
/* … */
<?php else: ?>
/* … */
<?php endif; ?>
There's nothing weird per se, it's because you're in a separate code block, that's the simplest way to put it. Nothing is open at the time of you "Leaving PHP", so when you go back into it there is no context.
Consider your code like this (of course consider it as pseudo-code just to emphasise the point):
if (isset($_SESSION["user_id"])) {
// ....
}; else {
// ....
}
Breaking in/out of PHP can be tricky at times, and managing it like you want to in your first example doesn't really make very much sense.
You might want to consider using this, which would put your transition to the else block on a single line anyway:
<?php if (isset($_SESSION["user_id"])): ?>
<h2>Welcome, <?php echo $_SESSION["user_id"]; ?></h2>
<?php else: ?>
<form action="" method="post">
<label for="user">User ID</label>
<input type="text" id="user" />
<label for="password">Password</label>
<input type="password" id="password" />
<input type="submit" value="Login" />
</form>
<?php endif ?>
At the end of the day whilst PHP is pretty flexible I wouldn't expect it to allow you to do what you're wanting. That would allow for an else block to be added miles away which may not be the intention at all.
<?php } ?>
^^
When PHP parses a file, it looks for opening and closing tags, which are which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.
When the PHP parser comes to this line it executes the if block only. After that PHP parser tried to parse the next block of code(else part) but here it start with else { and because of that else is separated from if and produces error.
Related
I'm quite new to PHP but used to some other programming languages (e.g JAVA,Python). Recently I had a closer look to the Login-Script panique/php-login-advanced (https://github.com/panique/php-login-advanced) which I find is a really good way to learn some useful PHP-structures.
Unfortunately there is one thing, i don't understand and which gives me quite a headache: all starts from "index.php" whih includes "login_manager.php" (used, among others, to create a new Login-instance from "classes/Login.php").
// create a login object. when this object is created, it will do all login/logout stuff automatically
// so this single line handles the entire login process.
$login = new Login();
If you aren't logged in, you can register yourself, which leads you to "views/register.php". In this file there is a POST-form, calling the same file again:
<?php include('_header.php'); ?>
<!-- show registration form, but only if we didn't submit already -->
<?php if (!$registration->registration_successful && !$registration->verification_successful) { ?>
<form method="post" action="register.php" name="registerform">
<label for="user_name"><?php echo WORDING_REGISTRATION_USERNAME; ?></label>
<input id="user_name" type="text" pattern="[a-zA-Z0-9]{2,64}" name="user_name" required />
<label for="user_email"><?php echo WORDING_REGISTRATION_EMAIL; ?></label>
<input id="user_email" type="email" name="user_email" required />
<label for="user_password_new"><?php echo WORDING_REGISTRATION_PASSWORD; ?></label>
<input id="user_password_new" type="password" name="user_password_new" pattern=".{6,}" required autocomplete="off" />
<label for="user_password_repeat"><?php echo WORDING_REGISTRATION_PASSWORD_REPEAT; ?></label>
<input id="user_password_repeat" type="password" name="user_password_repeat" pattern=".{6,}" required autocomplete="off" />
<img src="tools/showCaptcha.php" alt="captcha" />
<label><?php echo WORDING_REGISTRATION_CAPTCHA; ?></label>
<input type="text" name="captcha" required />
<input type="submit" name="register" value="<?php echo WORDING_REGISTER; ?>" />
</form>
<?php } ?>
<?php echo WORDING_BACK_TO_LOGIN; ?>
<?php include('_footer.php'); ?>
Now I don't understand where this $registration instance comes from?! Of course it should be an instance of "classes/Registration.php" which explains the further processing using the constructor of the class:
public function __construct()
{
session_start();
// if we have such a POST request, call the registerNewUser() method
if (isset($_POST["register"])) {
$this->registerNewUser($_POST['user_name'], $_POST['user_email'], $_POST['user_password_new'], $_POST['user_password_repeat'], $_POST["captcha"]);
// if we have such a GET request, call the verifyNewUser() method
} else if (isset($_GET["id"]) && isset($_GET["verification_code"])) {
$this->verifyNewUser($_GET["id"], $_GET["verification_code"]);
}
}
But where is the connection here? I searched the complete project with all files and I could not find something like "new Registration()" and even the $registration variable is never set (to my knowledge). So as the script works without problems, there needs to be some trick i don't know.
I also thought it could be set in the "_header.php" but in this file there is only some error-output:
// show potential errors / feedback (from registration object)
if (isset($registration)) {
if ($registration->errors) {
foreach ($registration->errors as $error) {
echo $error;
}
}
if ($registration->messages) {
foreach ($registration->messages as $message) {
echo $message;
}
}
}
After submitting data from a form, I want to be redirected to a page, here's my code :
<form action="#result" method="POST">
<input name="zipcode" type="text" placeholder="Your ZipCode" />
<input name="zipcode_submit" type="submit" value="Send" />
</form>
<div id="result">
<?php
if(isset($_POST['zipcode_submit'])) {
header("Location: http://twitter.com");
}
?>
</div>
It does not work for me and I don't know why
Thanks for your help
try to shift the php code, above the form tag,i.e
<?php
if(isset($_POST['zipcode_submit'])) {
header("Location: http://twitter.com");
}
?>
above
<form action="#result" method="POST">
Have a look at php.net docs regarding the header function. It has to be the first output to the website. Put your form at the end of the file.
<html>
<?php
/* this will produce an error, header must be the first output on the website */
header('Location: http://www.example.com/');
exit;
?>
You should place a exit; after the header statement, to fully prevent the following code from being executed.
I'm trying to get to grips with sessions as it's a part of PHP I'm not very good with. Could you help me in explaining what is happening here on the two pages that I have? It is giving an undefined index and I've no idea why.
Thanks
File 1
<strong>Test Form</strong>
<form action="test2.php" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['picturenum'] = $_POST['picturenum'];
}
?>
File 2
<?php
session_start();
echo $_SESSION['picturenum'];
?>
session_start() must go at the top of the page:
<?php
session_start();
// Opening <html>, etc goes below
?>
<strong>Test Form</strong>
<form action="test2.php" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['picturenum'] = $_POST['picturenum'];
}
?>
This works:
Form (teste1.php)
<?php
session_start();
// Opening <html>, etc goes below
?>
<strong>Test Form</strong>
<form action="test2.php" method"post">
<input type="text" name="picturenum"/> <!-- make sure you type something here -->
<input type="submit" name="Submit" value="Submit!" />
</form>
File 2 (test2.php)
<?php
if (isset($_POST['picturenum'])) {
$_SESSION['picturenum'] = $_POST['picturenum'];
echo $_SESSION['picturenum'];
}else{
echo "something wrong with the POST";
}
?>
As far as I can see, you're starting session after the form in the first file. The rule is: you should start the session before any echo or any HTML output, even before a space. So, basically, session_start() should be your first line after <?php.
Then, how do you get to the second page? If you close the browser and then re-open it, the session of course won't persist and you'll get your undefined index.
Please comment on this if you need any further explanations.
I have the following on a php page which does not want to work correctly. I am not sure if i'm not doing the conditional correctly or not? It appears to show both sets of content irrespective if the 1st condition is true or not, any ideas?
<? if(!isset($_SESSION['exhibitor_logged_in']) || $_SESSION['exhibitor_logged_in'] != true): ?>
<p>Please log in with the password.</p>
<form name="exhibitor_login" method="POST" action="">
<div>
<label for="password">Password:</label>
<input type="password" name="password" />
</div>
<div>
<label for="submit"> </label>
<input type="submit" name="submit" value="LOG IN" />
</div>
</form>
<? else: ?>
<p>Logged in</p>
<? endif; ?>
change all of you <? to <?php php isn't being parsed. You can confirm by viewing source on the web page.
Change all <? to <?php,
or modify your php.ini file, change short_open_tag = Off to short_open_tag = On
but short open tags are not recommended, you can see Are PHP short tags acceptable to use?
Using || will make and or comparation between the two statment, i think in your case you should use && because it should match both statment
if(!isset($_SESSION['exhibitor_logged_in']) && $_SESSION['exhibitor_logged_in'] != true):
I am getting the following error:
Parse error: syntax error, unexpected '<' in /home/u7mtb69/public_html/wpsitehrhhw/wp-content/themes/hrhhw.1.1/functions.php on line 1201
I am trying to add meta boxes to a custom post section in my function.php file.
The file works great, until I add the <div> container, then I get an error - but the container looks correct.
Can anyone tell me what the issue is?
// Add WHEELS meta boxes
add_action( 'add_meta_boxes' , 'wheel_meta_boxes' );
function wheel_meta_boxes() {
add_meta_box(
'wheel_info',
__( 'Wheel Info'),
'wheel_info_div',
'wheels'
);
}
function wheel_info_div( $post ) {
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'wheel_noncename' );
}
// WHEELS fields for data entry
<div>
<label for="tire_code">
<?php _e("Tire Code");?>
</label>
<input type="text" name="tire_code" value="<?php echo get_post_meta($post->ID, 'tire_code', true);?>" />
<br>
<label for="tire_name">
<?php _e("Tire Name");?>
</label>
<input type="text" name="tire_name" value="<?php echo get_post_meta($post->ID, 'tire_name', true);?>" />
<br>
<label for="tire_bname">
<?php _e("Tire Brand");?>
</label>
<input type="text" name="tire_bname" value="<?php echo get_post_meta($post->ID, 'tire_bname', true);?>" />
<br>
<input type="button" value="Create" id="create" />
<input type="button" value="Replace" id="replace" />
</div>
// NEXT
// END
?>
The problem is that you put HTML inside a PHP block. Don't do that.
You must remember that PHP and HTML are completely distinct. There is no relationship whatsoever, other than that the HTML your browser sees is the result of a PHP interpreter running your PHP script (which may include some hard-coded HTML to keep in the output, like your <div>). So when you see a PHP error, it's not going to be HTML at fault.
Wrong:
<?php
$somePHPCodeHere = 3;
<p>Some HTML here.</p>
?>
Right:
<?php
$somePHPCodeHere = 3;
?>
<p>Some HTML here.</p>
In other words: You just need to add the closing php tag ?> right after this line:
// WHEELS fields for data entry
?>