I have a project that works on a test server but stopped working when moved onto another.
Mainly I think it's the PHP but I ma here to ask your opinion.
So my main page is a HTML for login that has a form with POST that calls a php script file, like this:
<form method="POST" action="prologin.php">
Name: <input type="text" name="nam"><br>
Password: <input type="password" name="pas"><br>
<input type="submit" value="Login" data-inline="true" data-icon="gear">
</form>
An the prologin.php file looks like this:
<?php
session_start();
include 'mycon.php';
$nume1=mysql_real_escape_string( $_REQUEST['nam'] );
$pass1=mysql_real_escape_string( $_REQUEST['pas'] );
$s = "SELECT * FROM uzers WHERE uzname = '$nume1' and pass = md5('$pass1')";
var_dump($s);
... followed by other validations and stuff.
When I run the HTML file and click the Submit button (Login), I receive an "Undefined" page in Chrome or Firefox, and when I "view the source" I see the sql above with empty values. That is:
string(56) "SELECT * FROM uzers WHERE uzname = '' and pass = md5('')"
What could be wrong?
Why does php not receive the REQUEST variables? Because that is what I assume that happens.
What can I do to fix it?
This happens when you first loading the page before submitting the form, at this case $_REQUEST or $_POST not exists, you can avoid this by checking the variables:
if(isset($_REQUEST['nam']) && isset($_REQUEST['pas'])){
include 'mycon.php';
$nume1=mysql_real_escape_string( $_REQUEST['nam'] );
$pass1=mysql_real_escape_string( $_REQUEST['pas'] );
$s = "SELECT * FROM uzers WHERE uzname = '$nume1' and pass = md5('$pass1')";
//other staff
}
Strip your php down until you find the offending code. I would replace instances of $_REQUEST for $_POST and echo them when the page is loaded to make sure that they're being passed OK. The fact that your query echos out with blank parameters makes me think that $_REQUEST is an issue..
<?php
echo $_POST['nam'];
echo $_POST['pas'];
at the beginning, and see what happens when the page is processed..
Apparently I should have added this to the FORM:
data-ajax="false"
in order to prevent jQuery mobile from hijacking my submit behavior.
After I added this to the form , POST is working properly.
Thank you for your time and ideas but they did not help me solve my problem. However you might find this info useful in the future.
Related
I have a script that pulls an XML page and uses a form to update and save the values back. When I click the submit button it works, but then the page loads blank. I just want the page to refresh. There are about 100 different threads on this, and nothing I have tried has worked to resolve the issue. Out of curiosity, I just tried to run the window.location script and nothing else, and this piece actually doesn't work at all.
<?php
//if (isset($_POST['submit'])) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//$ctstatus->nodeValue = $_POST['ctstatusform'];
//htmlentities($xml->save('test.xml'));
echo '<script type="text/javascript">window.location="http://google.ca";</script>';
}
?>
The inner contents of the form don't really matter at this point, I just want it to refresh the page after I hit the submit button.
I previously used isset but from reading it seems like that's obsolete, and my form action="" used to be blank. Either way my XML save works, but nothing to refresh the page. I also tried header and that didn't work either.
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input class="save" name="submit" type="submit" value="Save" />
</form>
Out of curiosity I tried an onClick function with a timer and this does work but it's not ideal at all, especially because the page could technically refresh before the POST is finished writing the file. I'd rather know why the echo doesn't execute.
PHP redirect would most likely be preferable to JavaScript redirect.
Typical structure when posting back to same page:
<?php // cannot be any output before this (space, linefeed, etc)
if(isset($_POST['submit']) {
// do stuff with the submission
header('Location: http://google.ca');
exit;
}
// does your script need to do some other data retrieval or calculation? do it here.
?>
<html>
... snip ...
<form method="post">
... snip ...
<input class="save" name="submit" type="submit" value="Save" />
</form>
Following this simple structure for procedural scripts--
Deal with user input / redirect
Do logic (collect, manipulate data)
Show output (using php only to insert variables and looping)
will help you avoid a lot of heartache and technical debt.
Okay, I have gotten this sorted out. It turns out that the problem was embarrassingly simple, but maybe will assist someone in the future. Along with reordering my code, as Tim suggested. I specified HTML as the DOCTYPE, and that worked to resolve the issue. I no longer need to worry about refreshing the page after submit, because it refreshes as it should automatically. Thank you to everyone who commented.
I have a html page where the user can input some text, it is then posted to a php file and then stored in a database.
<form action="postphp.php" method="post" enctype="multipart/form-data">
<center><input id="postTitleStyle" type="text" name="title" size="100" maxlength = "180" ></center>
<center><textarea id="postTextStyle" name="Text" rows="10"cols="96" maxlength = "40000"><?php echo $text;?></textarea></center>
<center><input id="postTagStyle" type="text" name="tags" size="100" maxlength = "900" ></center>
<center><input type="submit" class = "Post2" value="post"></center>
</form>
Above is my current code for posting the data in the text field to a php file. I want to be able to click a button that when clicked will not go to the php file it will be stored and then when the user clicks the submit button it is posted. For example the user clicks it, a one is stored and then sent later when the user clicks the submit button after they are finished filling in other details. Is this possible?
P.S. I want to avoid Javascipt as much as possible for the moment, so if there is a non-java way of doing it then it would be much appreciated.
Many thanks, Jack.
There are two easy solutions to this problem without using Javascript. I'm assuming by your wording that you can currently post a form, but you don't know how to do so without leaving the current page. That's what I'll be answering below, but please note that there is no way to post a form without reloading at all without Javascript.
Solution 1: Put the PHP code into the same page the form is on and change the form tag to: <form action="" method="post" enctype="multipart/form-data">
A blank action field will cause it to run the PHP on the current page. You will likely need to look into using isset($_POST['submit']) in PHP, which will check whether the submit button has been clicked on before running that particular PHP code. You can read more about that HERE.
Solution 2:
In the postphp.php file that's currently linked to in your action field of your form, you could use a PHP header that will redirect the user after the PHP code is ran.
E.g.
<?php
{All your form processing code goes here}
header('location: my_form_page.php');
?>
In this example, my_form_page.php is the page on which your form is on. You can read more about headers in the answer of THIS question.
Hopefully this helps a bit.
$title = $_POST['title'];
$text= $_POST['text'];
$tags = $_POST['tags'];
mysql_query("INSERT INTO `table_name` (`colname1`,`colname2`,`colname3`) VALUES ('$title,'$text','$tags')");
$id = mysql_insert_id();
if($id){
echo "inserted";
}else{
echo "Not inserted";
}
For this you need to use Ajax (JavaScript will be used) because you need a button which send data to server without form submission and page reload it can be easily achieved using Ajax.
I am working on PHP/MySQL project with a modest CMS.
I am working on a page called - index.php?page=personnel - where I am trying to update a personnel in the database.
On that page I have an included form with - action="?editpersonnel" method="post"
Meanwhile, in PHP:
if (isset($_GET['editpersonnel'])) {
... // update personell ...
header('Location: .');
exit();
... idea being that when the form is submitted the script updates the personnel and then redirects back to itself.
Unfortunately, when I submit the form, instead of reloading the desired 'index.php?page=personnel' the script simply ads 'editpersonnel' to index.php so I end up with
index.php?editpersonnel
And by the way, none of the updates happen either...
Actually, it completely disregards everything after - if (isset($_GET['editpersonnel'])) - so I guess that's were the problem is...
Any ideas what might be causing such a behavior?
p.s.
Form is dynamically populated, so this is its page source:
Name:
Email:
Set password:
You need to use $_GET to be able to use something like that. Change your form's method to GET.
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
<input type="text" name="editpersonnel" />
<input type="submit" value="submit">
</form>
Something like that should work.
You are passing the form data by POST method and then trying to access them via GET
You have two possible solutions, see which one is more suitable for your case.
Change form method to GET
<form action="?editpersonnel" method="GET">
OR
Change access method in PHP to POST
if (isset($_POST['editpersonnel'])) {
... // update personell ...
header('Location: .');
exit();
}
I've created a form on my page and from the tutorials im following it says I have to have a second page with all the php processing in, is it not possible to keep the php on the same page and when the user hits submit the form is sent?
Why isnt this working on my process page? It doesnt echo anything :S
<?php
$kop = mysql_real_escape_string($_POST['severity']);
$loc = mysql_real_escape_string($_POST['location']};
$summary = mysql_real_escape_string($_POST['summary']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
echo $kop;
echo $loc;
echo $summary;
echo $name;
echo $email;
?>
You can check in the same file if the submit button is pressed. (http://pastebin.com/8FC1fFaf)
if (isset($_POST['submit']))
{
// Process Form
}
else
{
// Show Form
}
Regarding form checking, you can save the user input, and if one of their data is unvalid you can echo the old data back and show the form again.
edit: As PeeHaa stated, you need to leave the action blank though ;)
Yes it is possible. As adviced by Pekka, it's not really suggested to do so. But here is how it can be done.
Simply using the isset method to check if the form has been posted. So say in your form you have the follwing input:
<input type="text" name="age" />
At the top of your php script you can use the following to know if the form has been submitted and thus process it.
<?php if(isset($_POST["age"])) {
//do processing
} ?>
Hope this helps ;)
It is possible to let the same script process the form.
If you want to do this just leave the action blank.
However If you want to process the form without the page being reloaded you have to use Javascript.
is it not possible to keep the php on the same page and when the user hits submit the form is sent?
It sounds like you are describing AJAX. Example: http://www.elated.com/res/File/articles/development/javascript/jquery/slick-ajax-contact-form-jquery-php/
This is using the jQuery framework - there are a number of frameworks for doing this (such as YUI) which could do this equally as well. You could write this yourself to learn how it all works, but IHMO this would be reinventing the wheel. Here is a decent tutorial on creating AJAX forms with jQuery:
http://www.elated.com/articles/slick-ajax-contact-form-jquery-php/
<form name="myfrom" action="" method="post">
Username: <input type="text" name="user" id="username" />
<input type="submit" name="submit_form" value="Submit" />
</form>
<?php if($_POST['submit_form'] == "Submit"){
echo "do something";
}
?>
I basically have:
<form action="index.php" method="post">
<input name="text" type="text" value="Insert text here" size="20"/>
<input name="submit" type="submit" value="Submit" />
</form>
My PHP code then checks if submit is pressed:
if(isset($_POST['submit'])) {
$newDbValue = $_POST['text'];
$sql = ("
UPDATE `pseudo_tableName`
SET TEXT = '".$newDbValue."'
WHERE name = 'pseudo_fieldName' LIMIT 1
");
//SQL-query run by php function in separate class.
}
And as I understand it, if the form data is submitted it sends the user back to index.php?
But what mine does is it fails to update with new values and sends me back to index.php as if there was nothing wrong!
If I leave action="<?php $PHP_SELF; ?>" or action="" in the form, it updates when I reload the page (not F5 - click in address-line and hit enter).
What I want this to be:
I hit submit, and it updates the DB, sends me to index.php.
How can this be achieved?
Remove action="" (making you reload the page and using same file you have you if in) in your form and change your if to this:
if(isset($_POST['submit'])) {
$newDbValue = $_POST['text'];
$sql = ("
UPDATE `pseudo_tableName`
SET TEXT = '".$newDbValue."'
WHERE name = 'pseudo_fieldName' LIMIT 1
");
header("Location: index.php");
exit;
}
All <form> elements require the action attribute. What you're describing probably means your PHP script isn't detecting the form data and thus isn't making the necessary changes to your MySQL database. The reasons behind this may depend on the browser you're using; to my understanding, if you press Enter (keyboard) the submit button value isn't included in the form (this is said to allow multiple submit buttons with different value and perform the correct function).
If I were you, I'd check for the actual text input in the form, not the button.
<?php
// check 'text' exists and make sure it isn't blank.
if(isset($_POST['text']) && $_POST['text']!='') {
// do MySQL updating here.
header("Location: index.php"); // send back to index.php after updating.
}
?>
Because the most efficient method to redirect is to use header(); you're required to write your PHP form-checking script before the HTML form (because headers can't be send half-way through the actual document) so maybe at the top of the page before sending the HTML, additionally that way you can control what HTML is sent based on the processed data.
Have you tried setting:
<?php
if (isset($_POST['text']) AND !empty($_POST['text'])) {
// ...
}
?>
Instead of the other input?
The action='index.php' will send you back to index.php, so be sure that your PHP is above the form (and your HTML, for that matter). I also gues that you have set up your mysql_connect and you mysql_query functions.
Do you have error_reporting in php.ini on? What does the MySQL query result set say when you var_dump() the variable?
<?php
$sql = mysql_query($newDbValue));
var_dump($sql);
?>
That could really help you out.
The "cannot send header information - headers alredy sent" is because you have characters sent to the browser before the redirect() function - nothing can be sent to the browser (even whitespace!) if you're gonna use that, so be sure that the first thing in the document is your ?> tag, and your file encoding is UTF-8, not UTF-16 BOM or something crazy like that.