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):
Related
So i have an post input where i submit data something simple.
<form method="post" action="result.php">
<input type="url" name="url" class="form-control" placeholder="http://example.com/">
<input type="submit" name="submit" />
</form>
After the html code is going to be executed a php code which echo success or something like this that doesn't matter.
But i have a problem when i include('submit.php') it's going to show also the input and i don't want this.
How i can do that to don't show the input on result.php?
If you want it to be user-specific, you can try to use cookies or sessions like this:
index.php
<?php
session_start();
?>
<?php if(!isset($_SESSION['show_button']) && !$_SESSION['show_button'] ){ ?>
<!-- Button logic here... -->
<?php } ?>
result.php
// If the url has been entered, it returns a false from empty()
$_SESSION['show_button'] = empty($_POST['url']);
<form entype="multipart/form-data" method="GET" action="">
<div class="box-body">
<input class="form-control input-lg" name="keyword" type="text" placeholder="Masukkan kata kunci">
</div>
<div class="box-body">
<input value="1" type="checkbox" class="minimal" name="queryexp" />
Gunakan query expansion
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary" value="Submit">Submit</button>
</div>
</form>
Hi hello I want to ask a simple question. The code above is search.php,
I want to send the form to a different page based on if the checkbox is checked or not. If the checkbox is checked it will be directed to resqueryexp.php, but if not it will be directed to result.php
I have been trying to adding this code but it doesn't work.
<?php
if (isset($_GET['queryexp'])){
header("Location: resqueryexp.php");
}else{
header("Location: result.php");
}?>
Sorry for my bad English and Thanks in advance.
<?php
if ( isset( $_GET['submit'] )) {
if ($_GET['queryexp'] == 1 ){
header("Location: resqueryexp.php");
exit;
}
else
{
header("Location: result.php");
exit;
}
}
?>
<html>
<head><title>test</title>
</head>
<body>
<form enctype="multipart/form-data" method="GET" action="">
<div class="box-body">
<input class="form-control input-lg" name="keyword" type="text" placeholder="Masukkan kata kunci">
</div>
<div class="box-body">
<input value="1" type="checkbox" class="minimal" name="queryexp" />
Gunakan query expansion
</div>
<div class="box-footer">
<button type="submit" name="submit" class="btn btn-primary" value="Submit">Submit</button>
</div>
</form>
This code won't run here at SO, but this is how it may work on your webserver. The important part is to test if the form was submitted. So, in this case, I gave the submit button the name of "submit" and then tested with PHP to see if the form was even submitted. If the form is submitted and if the checkbox is checked, the redirect via header() occurs. Otherwise, if the checkbox is unchecked, then the redirect occurs via header to result.php. You may avoid header issues by making an adjustment to you PHP.ini settings and adding this line "output_buffering = On".
Note: usually a form with the enctype attribute having a value of "multipart/form-data" involves submitting a file and under such circumstances the method attribute should be a POST request instead of a GET; see MDN.
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.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
http://php.net/manual/en/function.header.php
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.
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.
I Have a form which when submitted needs to go to the page and then show one of 4 hidden divs depending on the page.
Here is the form
<form>
<input id="place" name="place" type="text">
<input name="datepicker" type="text" id="datepicker">
<input type="submit" name="submit" id="submit" />
</form>
Here is the page
<div id="brighton">
<p>Brighton</p>
</div>
<div id="devon">
<p>Devon</p>
</div>
<div id="search">
<p>search</p>
</div>
<div id="variety">
<p>variety</p>
</div>
So if Brighton is typed into the place input i need the form to submit the page and show the Brighton div and if Devon is typed in to show the Devon div etc and if the 2/12/2012 is typed into the date picker input and Brighton into the place input it goes to the page and shows the variety div.
i also need it so if the 1/12/2012 is typed in to the date picker input the page redirects to the page show.html.
any help would be greatly appreciated
thanks.
This is easy if you know PHP at all. It looks like you need a good, easy start. Then you will be able to achieve this in seconds.
Refer to W3SCHOOLS PHP Tutorial.
To achieve what you have mentioned, first make the following changes in your form:
<form action="submit.php" method="post">
<input id="place" name="place" type="text">
<input name="datepicker" type="text" id="datepicker">
<input type="submit" name="submit" id="submit" />
</form>
Create a new file called submit.php and add the following code:
<?php
$place = $_POST['place'];
$date = $_POST['datepicker'];
if ($date == '1/12/2012') {
header('Location: show.html');
exit;
}
?>
<?php if ($place == 'Brighton''): ?>
<div id="brighton">
<p>Brighton</p>
</div>
<?php elseif ($place == 'Devon'): ?>
<div id="devon">
<p>Devon</p>
</div>
<?php elseif ($place == 'search'): ?>
<div id="search">
<p>search</p>
</div>
<?php elseif ($place == 'Variety'): ?>
<div id="variety">
<p>variety</p>
</div>
<?php endif; ?>
Now the above example is not the complete solution, but it gives you an idea as to how you can use if-then-else construct in PHP to compare values and do as desired.
Post your form to a php page and then check the posted form parameters to determine which div to show.
<?php
if ($_POST["place"] == "Brighton") {
?>
<div id="brighton">
<p>Brighton</p>
</div>
<?php
} else if ($_POST["place"] == "Devon") {
?>
<div id="devon">
<p>Devon</p>
</div>
<?php
}
?>
Do that for each div and parameter combination. Make sure you set the "method" attribute on your form to "post":
<form action="somepage.php" method="post">...</form>
In the resulting HTML you will only see the one that matches the form parameter.