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

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

Related

HTML button submit not activating php code

I am having an issue with my html forms not submitting properly. I am new to forms and have been trying to get a login script going but it has been failing. I am using chrome mostly. Here is the super dumbed down code that hopefully demonstrates my issue. I am guessing what is wrong in this code is in the extended code. This is the website I used for my login coding: login account setup site mainly for reference
<?php
//this is here for testing
session_start();
//this shows
echo "something";
//none of these show
if(isset($_POST['submit'])){
echo "something";
echo "something";
echo "something";
echo "something";
}
if(isset($_POST['submit2'])){
echo "something";
echo "something";
echo "something";
echo "something";
}if(isset($_POST['submit3'])){
echo "something";
echo "something";
echo "something";
echo "something";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<footer>
<!-- I have tried it with and without an action -->
<form action="button_test.php" method="post" id="this" style="width: 100%; height: 100%; background-color: darkblue;">
<button type="submit" name="submit" id="submit" class="button" value="Submit">this is something</button>
<button type="button" name="submit2" id="submit2" class="button" value="this">this is something2</button>
<input type="button" name="submit3" id="submit3" class="button" value="true"/>
</form>
</footer>
</body>
</html>
The first button predictably reloads the page, while the others do nothing. This is the extended main code that I need working. Once again, I am guessing the code above probably explains what is wrong with the code beneath. The code beneath is primarily there to give context and more accurate answers.
<?php
//require once the DatabaseController.php file,
require_once 'Controllers/DatabaseController.php';
echo "this happend1";
// Creates a data connect object that creates a connection to
// a database
$connection = new DataConnect;
//Run the user_load function to set the user
$connection->user_load();
echo "this happend2";
/*
* Runs the in_loggedin function from a
* user object and checks if it is blank
*/
if($user->is_loggedin()!="")
{
echo "this happend3";
//return user to logged in home page if
//they are logged in
$user->redirect('index.php');
}
echo "this happend4";
/*
* THIS DOES NOT WORK FOR SOME REASON
*/
if(isset($_POST['btn-login']))
{
echo "this happend5";
//Load the variables with the new form data that has been executed
$uname = $_POST['txt_uname_email'];
$umail = $_POST['txt_uname_email'];
$upass = $_POST['txt_password'];
//If the user->login call is successful, go to home fully logged in
if($user->login($uname,$umail,$upass))
{
//Go to home, fully logged in
$user->redirect('index.php');
}
else
{
//Error
$error = "Wrong Details !";
}
}
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title> Josiah</title>
<link href="Assets/Stylesheets/styles-home.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
include_once 'header.php';
?>
<!-- recent blog post: contains the most recent blog post-->
<div id="main-content" class="content">
<div id="main-content-inner" class="inner-content">
<!-- this is the container for the form -->
<div class="form-container">
<!-- create a form that uses the "post" method which allows php to do $_POST on
corresponding variable names -->
<form method="post">
<h2>Sign in.</h2><hr />
<!-- checks if there was an error. If the error variable is set -->
<?php
if(isset($error))
{
?>
<!-- displays the error -->
<div class="alert alert-danger">
<i class="glyphicon glyphicon-warning-sign"></i> <?php echo $error; ?> !
</div>
<?php
}
?>
<!-- this is a form-group -->
<div class="form-group">
<!-- this is a text input. -->
<input type="text" class="form-control" name="txt_uname_email" placeholder="Username or E mail ID" required />
</div>
<!-- this is a form-group -->
<div class="form-group">
<!-- this is a text input. -->
<input type="password" class="form-control" name="txt_password" placeholder="Your Password" required />
</div>
<!-- splits parts of the form -->
<div class="clearfix"></div><hr />
<!-- this is a form group holdng the submit button -->
<div class="form-group">
<!-- the button interestingly has multiple classes -->
<button type="submit" name="btn-login" class="btn btn-block btn-primary">
<!-- splits parts of the form. Dont know specifically what this does. -->
<i class="glyphicon glyphicon-log-in"></i> SIGN IN
</button>
</div>
<br />
<label>Don't have account yet ! Sign Up</label>
</form>
</div>
</div>
</div>
<!-- footer: contains social media, site map, and an up arrow-->
<?php
include 'footer.php'
?>
</body>
</html>
Since php://input shows the form value (according to the test I had you run in comments), we know that PHP is properly receiving the data.
By default, PHP will place request parameters in $_GET and $_POST as appropriate. In your case this doesn't seem to be happening. I suggest you look at your php.ini file and add the setting below:
variables_order = "GPCS"
There may already be a variables_order line, so just edit it in that case (documentation).
So after some searching I forgot to disclose that I was using PHPstorm. The reason why this did not occur to me is that PHPstorm is not installed on another computer that I have been using, however since I use Intellij on both computers, this bug in PHPstorm transferred regardless since Intellij will use PHPstorm plugins. It seems that 10.1-10.3 this bug persists where POST remains empty. I am not sure if it was fixed so I moved over to Apache and have it doing the web serving instead. Everything works, or at least works better. The 2 solutions are either wait for PHPstorm to fix this bug, or better move over to Apache.
link to site post

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 function Not Displaying form

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.

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