No Post Data to Controller - php

I have a simple form built in a CodeIgniter view:
<html>
<head>
<title></title>
<base href="<?php echo site_url(); ?>" />
</head>
<body>
<?php echo form_open('main/testformout'); ?>
First name: <input type="text" name="fname" /><br>
Last name: <input type="text" name="lname" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
EDIT: Here is the HTML output of the form above:
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="http://www.mypage.com/main/testformout">
First name: <input type="text" name="fname" /><br>
Last name: <input type="text" name="lname" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
I have tried it with and without the form helpers but in this example I am using the form_open helper.
When hitting the submit button no POST data makes it to the Controller.
Controller Code:
public function helperin()
{
$this->load->view('test/helperin_v', NULL);
}
public function testformout()
{
print_r($_POST);
echo "<br />";
echo $_SERVER['REQUEST_METHOD'];
echo "<br />";
var_dump($this->input->post());
$this->load->view('test/out_v', $viewData[$_POST]);
}
When the system and application folders are loaded at the same level as the public_html folder I get this result:
GET
bool(false)
When the system and application folders are loaded inside the public_html folder (I know this is not secure) I get this result:
Array ( )
GET
bool(false)
Also, the code works perfect on my local machine but does not work on when uploaded to Bluehost server.
Here is my htaccess file (because I've done enough research to know this may be of value).
RewriteEngine On
RewriteCond $1 !^(index\.php|public|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Bluehost service people are saying it is because of a redirect issue and they consider the matter closed but as I am not doing a redirect that leaves either the CodeIgniter framework or their server.

Related

CodeIgniter : error 403 after submit form

When I try to send my form with the base_url() method (so I click on the submit button), instead of the result of my request, I have a 403 error on the other page (forbidden acces), on the page generated by my controller.
I use a Wampp Server, the version 3.1.8 of CodeIgniter. I've also add url in the autoload file.
I have .htacess file with that :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
View page :
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" action="<? echo base_url();?>form_validation">
<label> Name </label>
<input type="text" name="name" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
Can you help me ? Thanks a lot
Hope this will help you :
NOTE : Assuming that default controller is welcome in route.php if not replace welcome with yours
In controller : you have method form_validation and you give in form action only validation :
public function form_validation()
{
echo 'OK';
}
Your form view is just like this :
REPLACE :
/* note missing php error in action*/
<form method="post" action="<? echo base_url();?>form_validation">
With :
/*add controller name also in action,
assuming your default controller is Welcome*/
<form method="post" action="<?php echo base_url('welcome/form_validation');?>">
View should be like this :
<form method="post" action="<?php echo base_url('welcome/form_validation');?>">
<label> Name </label>
<input type="text" name="name" />
<input type="submit" name="submit" value="submit" />
</form>
route.php should be like this :
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
NOTE ALSO : .htaccess should be in project folder , in your case CI folder

POST variable doesn't echo in function

I am currently learning the most basic PHP ever. I have 5 files.
index.php:
<html>
<head>
<title>Budget Calcule</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>Put in your: - </h2>
<form action="functions.php" method="post">
<h3>Income</h3>
<label>Salary: <input name="salary" type="text" /></label><br />
<h3>Outgoings</h3>
<label>Living: <input name="living" type="text" /></label><br />
<label>Insurance: <input name="insurance" type="text" /></label><br />
<label>Communication: <input name="communication" type="text" /></label><br />
<label>Loan: <input name="loan" type="text" /></label><br />
<label>Food & Drink: <input name="foodAndDrink" type="text" /></label><br />
<label>Entertaintment / Shopping: <input name="entertainmentOrShopping" type="text" /></label><br />
<label>Transport: <input name="transport" type="text" /></label><br />
<label>Other: <input name="other" type="text" /></label><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
this is my functions.php:
<?php
include('variables.php');
if(!($_POST['Submit'])){
if(isset($_POST['salary'])){
header('Location: output.php');
return $_POST['lon'];
}else{
echo "All fields are required";
}
}
?>
this is my variables.php:
<?php
$salary= $_POST['salary'];
$living= $_POST['living'];
$insurance= $_POST['insurance'];
$communication = $_POST['communication'];
$loan = $_POST['loan'];
$food = $_POST['food'];
$entertaintmentOrShopping = $_POST['entertaintmentOrShopping'];
$transport = $_POST['transport'];
$other= $_POST['other'];
?>
this is my output.php file:
<?php
include('outputFunction.php');
?>
<html>
<head>
<title>Output.php</title>
</head>
<body>
<?php myText(); ?>
</body>
</html>
and last but not least, this is my outputFunction.php file:
<?php
include('variables.php');
function myText(){
echo "Your salary per month is: " . $_POST['salary'];
}
?>
Now you're thinking "why have he split up his code in different files?" Well first of all, I split the variables from functions.php because I wanted outputFunctions.php to get the variables from variables.php so i could echo my `$_POST['salary']; . The function myText(); outputs the text just fine, but it doesnt output the $_POST['salary'];.
I do not know why it doesnt work, I just wonder if you could be my extra eyes and see if I've done some mistake.
PS! Don't down vote my question just because you think it's stupid. I am having problem with this issue and been working on it for hours without advancing anywhere.
A few things:
You don't need to include a variables.php file. The variables you're accessing are global and you're just creating duplicates that aren't being used. They also go away after the page changes since you're re-declaring them each page load.
You are also trying to call a variable that doesn't exist when you reference $_POST['lon'] instead of 'loan'.
And finally to actually answer your question:
Your myText() function is referencing a variable that is not there anymore.
You need to merge functions.php and outputFunction.php and output.php into one file so the variables aren't lost and all the processing is done without opening a new file each time. I can see your original concept for separated files but an output file is going to be the file to process the input data from the form.
Now in your newly merged output.php, you should have something resembling this:
<html>
<head>
<title>Output</title>
</head>
<body>
<?php
if(isset($_POST['Submit'])) {
if(isset($_POST['salary'])) {
echo "Your salary per month is: " . $_POST['salary'];
}
} else {
echo "All fields required.";
}
?>
</body>
</html>
This means only two files - your form page and this page.
A few more tips:
If you want to check if the form was submitted, it has look something like this:
if(isset($_POST['Submit'])){ ... }
Also, you should add a name="" attribute to your submit-Button:
<input type="submit" name="Submit" value="Submit" />
And what is the variables.php for? You don't use any of those variables.
When you redirect the user via header() the data that is stored in the $_POST array gets lost.
You could directly redirect to ouput.php
<form action="output.php" method="post">
And do something like this:
<?php
include('outputFunction.php');
if(isset($_POST['Submit'])) {
if(isset($_POST['salary'])) {
?>
<html>
<head>
<title>Output.php</title>
</head>
<body>
<?php myText(); ?>
</body>
</html>
<?php
} else {
echo "All field required";
}
}
?>
By the way you can always check what your $_POST contains with print_r($_POST);
This can be very useful for debugging.

Empty POST requests with Varnish cache

So I have a very simple login-form in html/php
login1.php
<?php include('settings.php'); ?>
<!DOCTYPE html>
<html>
<head>
<title>login</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form id="form1" name="form1" method="post" action="<?=$basehttp;?>/login_auth1.php">
<input name="ahd_username" type="text" id="ahd_username" size="35" maxlength="255" />
<input name="ahd_password" type="password" id="ahd_password" size="35" maxlength="35" />
<input type="submit" name="Submit" value="Login" />
</form>
</body>
</html>
login_auth1.php
<?php
include('settings.php');
print_r($_POST);
print_r(getallheaders());
if(isset($_POST['ahd_username']) && isset($_POST['ahd_password'])){
//database queries and stuff, send user elsewhere if login correct
}
?>
<!DOCTYPE html>
<html>
<head>
<title>login</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form id="form1" name="form1" method="post" action="<?=$basehttp;?>/login_auth1.php">
<input name="ahd_username" type="text" id="ahd_username" size="35" maxlength="255" />
<input name="ahd_password" type="password" id="ahd_password" size="35" maxlength="35" />
<input type="submit" name="Submit" value="Login" />
</form>
</body>
</html>
When running login1.php (in some) web browsers the POST in login_auth1.php will be empty. But when submiting the exact same form again from login_auth1.php it works just fine. What might be the cause of this error? How to debug further? FYI The server is running Varnish.
Link to output with headers for some calls can be found in this textfile.
Edit:
Have no hair left on my head now but I've figured out a way that "works". If I merge my loginfiles to a single file, put that file into a separate folder /login, removing the url in action tag completly (so it will be action="") and configure Varnish to
if (req.url ~ "/login"){
set req.backend = mybackend;
return (pass);
}
it will work. This feels like a really crappy "solution", especially the part with removing the string in the action tag but it's the only way that I've made it work. I've tried tons of different Varnish configurations, including return (pipe). Does anyone have any idea on how to get it to work properly? Or why this weird version above works?
A good and lasting practice, to replace your quick hack, is to not cache any post requests by:
if ( req.request == "POST" ) {
return (hit_for_pass);
}
Varnish' standard behaviour[1] (at least in 3.0, debian) is to pass POST request directly to backend:
sub vcl_recv {
#...
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
#...
}
So if default code is not reached (you call return(xxx) in your vcl_recv) you should take care of this kind of requests in your own VCL code, returning a pass or either a hit_for_pass as suggested by Clarence.
[1] https://www.varnish-cache.org/docs/3.0/reference/vcl.html#examples

url issues with codeigniter

I have an example at the following url, which sends the user to a basic form.
The code for the form is the following;
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
If I understand correctly, once the user presses "submit", it should go back to the controller named "form". But it does not work as expected.
I would want it to submit to index.php/form, and instead it goes to /index.php/index.php/form
Feel free to test out website at url above and see the issue by yourself.
I just looked at your source code and noticed this:
<form action="http://helios.hud.ac.uk/u0862025/CodeIgniter/index.php/index.php/form" method="post" accept-charset="utf-8">
Which displays two "index.php"s. This likely means you have "index.php" set in your base url AND index page configuration. You should remove the "index.php" from one of those sources, preferably from your base_url.
Check your base_url() in config file. If it is correclty, than you can write the form like this:
<?php echo validation_errors(); ?>
<?php echo form_open('controller_name/function_name');
// controller name write your name of controller
// and function name write your function
?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
<?php echo form_close(); // don't forget to close correct ?>
I have faced this situation once and the problem was i have included index.php in my base_url in ./application/config/config.php file please check and remove if present. You can also solve your problem by removing index.php from index_page key but it is not a good practice to include index.php in your base_url.

PHP form automatically refreshes whenever data is entered

I'm building my first PHP website (attempting to, anyway!), and I'm trying to create a contact form whose contents are submitted to me via email. I've got the email part down, but I'm having trouble getting the form ("contact.php") to accept data. It automatically refreshes as soon as I type a character in any field. Here's the relevant code:
<div class="contactform">
<form name="contactform" method="post" action="contact-receiver.php">
<fieldset><legend><strong>Required Information</strong></legend>
First Name: <input type="text" name="firstName" size="35" maxlength="30"/>
Last Name: <input type="text" name="lastName" size="35" maxlength="30"/>
Email: <input type="text" name="emailAddress" size="60" maxlength="55"/>
</fieldset>
<input type="submit" value="Submit"/>
<input type="reset" value="Reset"/>
</form>
</div>
This works fine when tested independent from the rest of the site. However, here's the context:
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<?php include 'header.php'; ?>
<?php
$id = $_GET['id'];
switch($id)
{
case 'main':
include 'storeinfo.php';
break;
case 'shop':
include 'inventory.php';
break;
case 'cart':
include 'cart.php';
break;
case 'contact':
include 'contact.php';
break;
default:
include 'error.php';
}
?>
<?php include 'footer.php'; ?>
</body>
</html>
"contact.php" works fine when displayed as a separate page, but won't accept any input when accessed as an include file. If I try to enter data in any of the fields, the page immediately refreshes after I type the first character, and the data is lost.
If anyone could point me in the right direction, I would really appreciate it!
EDIT
Disabling Javascript didn't work. I cleared my cache and restarted my browser (Firefox) just to be sure. While I'm working on that voodoo priest, here's the page source for index.php?id=contact:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Main</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<div>
<p><img src='headerimg.png' class='header'/></p><a href='index.php?id=main'>
<img src='mainbutton.png' class='nav1'</a><a href='index.php?id=shop'>
<img src='shopbutton.png' class='nav2'</a><a href='index.php?id=cart'>
<img src='cartbutton.png' class='nav2'</a><a href='index.php?id=contact'>
<img src='contactbutton.png' class='nav2'</a>
</div>
<div class="contactform">
<form name="contactform" method="post" action="contact-receiver.php">
<fieldset><legend><strong>Required Information</strong></legend>
First Name: <input type="text" name="firstName" size="35" maxlength="30"/>
Last Name: <input type="text" name="lastName" size="35" maxlength="30"/>
Email: <input type="text" name="emailAddress" size="60" maxlength="55"/>
</fieldset>
<input type="submit" value="Submit"/>
<input type="reset" value="Reset"/>
</form>
</div>
<div id = "footer">
<p>©2012</p>
</div>
</body>
</html>
It sounds like a Javascript-related problem.
Check and make sure you're not including any scripts which try to autocomplete, as being misconfigured might cause it to send a request upon key up which would cause the behavior you're mentioning.
An easy way to test this is to disable JavaScript in your browser and see if the issue continues. If it does, it means you have ghosts in your computer and should see a voodoo priest. If the issue doesn't persist, it means it's an issue with some JavaScript on your site.
Posting some contents of header.php will help, as well. OR, you could simply post the complete HTML page source once - that is, visit your index.php?id=contact page, hit view source, and show that here.

Categories