php function Not Displaying form - php

Im having a problem displaying a php function. Its for an admin log in form.
The Function Look Like this -
function displayAdmin(){
//test if login is valid
if (isset($_SESSION['adminLogin'])){
if($_SESSION ['adminLogin']=="valid"){
?>
<script type="text/javascript">location.replace('addproduct.php')</script>
<?php
}
else {
// test if login is invalid
// display error message and login form
if($_SESSION['adminLogin']=="invalid") {
echo "<div>Incorrect User ID and/or password provided</div>";
?>
<form name="adminLogin" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div id="sign_up_form">
<label><strong>Username:</strong> <input type = "text" name="userID" class="sprited"/></label>
<label><strong>Password:</strong> <input type="password" name="passWord" class="sprited"/></label>
<div id="actions">
<a class="close form_button sprited" id="cancel" href="#">Cancel</a>
<a type ="submit" name="adminSignin"class="form_button sprited" id="log_in" href="">Sign in</a>
</div>
</div>
</form>
<?php
}
}
?>
<form name="adminLogin" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div id="sign_up_form">
<label><strong>Username:</strong> <input type = "text" name="userID" class="sprited"/></label>
<label><strong>Password:</strong> <input type="password" name="passWord" class="sprited"/></label>
<div id="actions">
<a class="close form_button sprited" id="cancel" href="#">Cancel</a>
<a type ="submit" name="adminSignin"class="form_button sprited" id="log_in" href="">Sign in</a>
</div>
</div>
</form>
<?php
}
} // end of function
And on my page where I am wanting the function to sit the code looks like this -
<?php
session_start();
// Test that page title has been created
if (!isset($pageTitle)) {
$pageTitle = '<< Page title not set >>';
}
// include the myFunctions file
include('includes/myFunctions.php');
// test if login details have been keyed in
if(!empty($_POST["userID"])) {
// Store userID and passWord in local variables
$userID=$_POST["userID"];
$passWord=$_POST["passWord"];
// check database for valid customer
checkValidAdmin($userID, $passWord);
}
?>
and then -
<div id="sign_up">
<h3 id="see_id">Administration Log in</h3>
<span>Please sign in using the form below</span>
<div><?php displayAdmin(); ?></div>
<a id="close_x" class="close sprited" href="#">close</a>
</div>
I have searched long and hard for this problem but can not seem to find the issue, if the issue jumps out at anyone I would love to hear from you!
Thank you so much in advance!!

Definitely your $_SESSION ['adminLogin']=="valid" condition is not true. Check it is set properly or session_start(); is called at beginning of the script.

The problem is you did'nt return the html string.
You should assign the form to a variable, and at the end of the function , return this variable.

$_SESSION['adminLogin'] value is empty i.e. not set or null. that is the reason function inside condition not satisfied.

Related

PHP redirect form to URL not working

So I'm trying to use this http://www.formget.com/how-to-redirect-a-url-php-form/ as an RSVP form.
Ideally, entering the right code on (http://baby.engquist.com/invite/) will lead you to a google form. However, when I enter any code (right or wrong) and press the button, it simply refreshes back to the /invite page.
My code is as follows:
<p style="text-align: center;">
<form action="index.php" id="#form" method="post" name="#form">
<div class="row">
<div class="large-3 columns large-centered">
<div class="row collapse">
<div class="small-10 columns">
<input id="code" name="code" placeholder="Enter the code to RSVP." type="text" >
</div>
<div class="small-2 columns">
<input id='btn' name="submit" type='submit' class="button prefix" value='Go'>
</div>
</div>
</div>
</div>
<?php
include "redirect.php";
?>
</form>
</p>
And the included redirect.php:
<?php
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$code = $_POST['code'];
if($code ='show620')
{
// To redirect form on a particular page
header("Location:http://google.com/");
} else {
print "Oops that's not the right code. Try again!";
}
?>
Thanks so much for any help!
You should have action attribute pointing to file where you do processing after submitting. In your case its redirect.php
Use :
<form action="redirect.php" > ............
And dont include redirect.php at the bottom of the form.
You need to write ob_start(); on top of your page and die(); after header("Location:http://google.com/"); in redirect.php
The php header redirect only works if it's called from a page that is completely blank. You have to change your form action to "redirect.php" and simply get rid of the code at the bottom of your html.

Printing html page and doing some action using 1 button

I have a Print button which I want to do 2 actions when clicked:
Updating my database.
Printing the HTML page.
This is what I've done so far, but it's not working:
<form action="" method="POST">
<body >
<?php
$n=$_POST['ID'];
$a=implode("</br>",$n);
list($add, $ward) = explode("(!#!)", $a);
?>
<div id="container">
<p id="address">
<?php echo"$address";?>
</p>
<p id="ward">
<?php echo"$ward";?>
</p>
</div>
<input type="submit" name="Print" value="Print" />
<?php
if(isset($_POST['Print']))
{
?><script>javascript:window.print()</script><?php
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
}?>
<div id="footer">
</div>
</form>
After using this print button , my database is getting updated, but the print window is showing error(i.e the variables posted from other page are showing errors).
Can anyone please help me print and update at same time with this Print button?
If your page does not have it, add form tags. They are mandatory unless you want to AJAX the data. Also remove the semi-colon from the end of the sql
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php
if(isset($_POST['Print'])) {
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?><script>window.print();</script>
<?php } ?>
UPDATE: Perhaps you mean this, but I do not want to keep correcting HTML.
<?php
$n=$_POST["ID"];
$a=implode("</br>",$n);
list($add,$ward)=explode("(!#!)", $a);
?>
<body>
<div id="headerbg">
<div id="header-e1"><a align="left" href="escalationReport.php">Back </a>
</div>
<div id="header-e3"><a align="right" href="logout.php">Logout </a>
</div>
<h1><p>Issue Notice</h1>
</div>
<div id="container">
<p id="address">
<?php echo "$address";?>
</p>
<p id="ward">
<?php echo "$ward";?>
</p>
</div>
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php if(isset($_POST[ 'Print'])) {
mysql_query( "UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?>
<script>
window.print();
</script>
<?php } ?>
<div id="footer"></div>
</body>
you have an error in the update query, you have placed semicolon inside the query so please remove that so the query will be like this.
Make sure your form method is POST
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
Make sure you are using input type button inside the form tag. And use post method for form.

PHP: When echo a JavaScript alert, text input size decreases

I have a basic HTML form like this. When submitted, it checks for empty fields and alert the user that both fields are required. After alert, user is redirected to the same page but text input sizes decrease. How can I retain the same width, height after PHP "echo"?
Screenhot 1:
Screenshot 2:
Screenshot 3:
Controller:
function validate_user_pass() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username','Email', 'required|max_length[30]');
$this->form_validation->set_rules('password','Password', 'required|max_length[32]');
if ($this->form_validation->run() == FALSE ) {
//$this->load->view('login_error');
echo '<script type="text/javascript">alert("Required fields are empty!");</script>';
$this->index();
}
else {
$this->verify();
}
}
View:
<?php echo form_open('login/validate_user_pass'); ?>
<div class="login">
<div class="login-screen">
<div class="login-icon">
<img src="<?php echo base_url(); ?>assets/images/1374606542_newsstand_ios7_ios_7.png" alt="logo">
<h4>Hotel<small>Management System</small></h4>
</div>
<div class="login-form">
<img src="<?php echo base_url(); ?>assets/ico/logo_small.png" alt="logo">
<div class="control-group">
<input type="text" class="login-field" name="username" value="<?php echo set_value('username'); ?>" placeholder="Enter username" id="login-name">
</div>
<div class="control-group">
<input type="password" class="login-field" name="password" value="" placeholder="Password" id="login-pass">
</div>
<input type="submit" class="btn btn-primary" value="Login">
</div>
</div>
</div>
<?php echo form_close(); ?>
it may be late to answer but same problem was with me. I overcome this by putting the error popup box out of the header. I searched everywhere and ended up here.
My code was something like this before:
<?php
//some code for verification and echo out/show error message
?>
<html>
<!--HTML code that has form-->
</html>
What I did was something like this:
<html>
<head>
<title></title>
</head>
<body>
<?php
//some code for verification and echo out/show error message
?>
<div>
<!--HTML code that has form-->
</div>
</body
</html>
This problem mostly exist because of echo present in header part. You can check your views for this and that may solve the problem.
This typically happens when a php error or var_dump() is on the page, but you may not see it because it is hidden under a fixed header div, for example.
Look at your View source & check if there aren't php errors on the page

PHP sessions problems

I'm using sessions to save what ever the user types in the form and what ever they do type will be displayed on the other pages.
It was working perfectly fine but after all the server uploads and such my code has completely done one on me and i'm lost.
Can somebody see if they can spot a mistake? I need fresh eyes.
HTML.
<div id="form"><!--Form Start-->
<form action="home.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
PHP.
<?php
session_start(); // declaring the variable
if(isset($_POST['fullName'])){ //setting the variable
if(empty($_POST['fullName'])){ //if empty dont to nothing but my wep page will reload
}else{ //if they have do this
$_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)
header("Location: home.php"); //then will direct the user to the home page (will also display name on each page)
}}
?>
Session on other pages
<div id="echo"> <!-- div ECHO start -->
<?php
echo $_SESSION['fullName']
?>
</div> <!--div ECHO end -->
$_SESSION['fullName'] = $_POST['fullName'];
session_register(fullName);
replace with this code try it
You'll need to add session_start() on whatever page you are redirecting to that is supposed to display the data.
Also, (I'm assuming you realize) what you posted doesn't have anything that would output the data, like:
<input type="text" name="fullName" value="<?php echo $_SESSION['fullName']; ?>"/>
You need to start session on other page as well and stop the script from setting that session. After header location you need to use exit here.
<?php session_start();?>
<div id="echo"> <!-- div ECHO start -->
<?php
echo $_SESSION['fullName'];
?>
you need use exit after header location :-
header('location: home.php');
exit;
Just change the div id form to other because it has a default and remove the empty function because you add isset functon.
Use this.
<div id="myform"><!--Form Start-->
<form action="home.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
PHP.
<?php
session_start();
if(isset($_POST['fullName']))
{
$_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)
header("Location: home.php");
exit();
}
?>
Session on other pages.
<div id="echo"> <!-- div ECHO start -->
<?php
session_start();
print_r($_SESSION);
echo $_SESSION['fullName'];
?>
</div> <!--div ECHO end -->
May be it helpful to you.If any problem then let me know.
You are "posting" the values to home.php, doing that you can't set $_SESSION['fullName'] = $_POST['fullName'] in the origin.
Change
<form action="home.php" method="post">
to
<form action="name_of_the_first_script.php" method="post">
$_POST['fullName'] does not exist before the redirect.
Here is how everything should look like (lest call the page index.php):
<div id="form"><!--Form Start-->
<form action="index.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
now after you hit submit the index.php will be reactioned and at this time with the $_POST request meaning that that the condition
if(isset($_POST['fullName'])){
will be true and the PHP code can be executed, setting the $_SESSION variable and redirecting you to home.php where you ca now read the $_SESSION previously set in index.php
Hope this can me more clear now! :)

submit form to page and depending on input show different div

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.

Categories