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.
Related
I am trying to pre-fill my form with data that is found in my .php file. Please, what am I doing wrong? The form field displays exactly what is found in between quotes.
<form id='registration' method="POST" action="m.php" >
Username:<br><input type="read-only" name="username" value="<?php echo htmlspecialchars($ss); ?>" /><br>
This is the .php file
<?php
session_start();
if($_SESSION["logged_in"] == true){
$ss=$_SESSION['username'];
}
else $ss="nu";
?>
The file containing the form html code should also be named and executed as php (and have the .php extension alongside the other one). Otherwise php doesn't get executed inside the html code and you end up with php code sent as-is to the browser.
The action attribute is used to inform the browser what page (or script) to call once the "submit" button is pressed, this is why you are not loading the username
You need to add that php script into the page the form is on as opposed to adding it to the forms processing functionality.
Here is a read on the action attribute
https://www.sitepoint.com/action-html-attribute/
let me know if I can be of further assistance but that change should get you up and running.
This is my admin panel code:
<form action="connectdb.php" method="post">
<input type="text" name="name">
<input type="submit">
</form>
So, It so, the code in connectdb.php will only run, if the "submit" button redirects a user to it. It will not run, if a user directly open /connectdb.php page.
Do I need to start some session, something like that?
Note: I am a newbie, so please explain in detail.
Since your form is using method="post"you can place the following code at the very beginning of your connectdb.php file:
<?php
if (empty($_POST)){
exit;
}
//The rest of your code goes here
This checks to see if the $_POST variable either does not exist or does exist but is empty. If this returns true that means your form was not submitted and a user went to the page directly. The script will then exit and a blank screen will be displayed.
Instead of displaying a blank screen, you may instead want to redirect to a different page such as this:
<?php
if (empty($_POST)){
header("Location: index.html");
exit;
}
//The rest of your code goes here
Whenever you do a redirect like this, it is important to place an exit; statement directly after it, otherwise your script could still process some of the other statements and send data to the browser that shouldn't be sent. This of course could be a security risk in some cases. An exit statement prevents this kind of security risk.
Not sure if you really need it, but you can add a name attribute like the following:
<input name="submit_button" type="submit">
So when you click this button a $_POST['submit_button'] variable will be created on the PHP side, and then you can use it to check if the button was clicked:
if(isset($_POST['submit_button'])){
// your code
}
<input type="submit" name="submit_btn">
Now in your connectdb.php check,
<?php
if(isset($_POST['submit_btn']))
{
//do your code
}
else
{
//redirect to your home page
}
?>
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 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.
This may sound noobish, but i have a code that submits a file to a database (and reads the file) whenever i hit submit, and everything works perfectly, except after i refresh the page it re-adds the last value I Selected. Here is the code where the problem lies :
<?php
mysql_connect("localhost","root","");
mysql_select_db("a1296556_data1");
if(isset($_POST['submit'])){
$name=$_FILES['file']['name'];
$temp=$_FILES['file']['tmp_name'];
move_uploaded_file($temp,"uploaded/".$name);
$url="http://www.bluejayke.com/edit/uploaded/$name";
}
?>
<form action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="Upload">
</form>
<iframe src='video.php' width=250 height=600></iframe>
<?php
if(isset($_POST['submit'])){
mysql_query("INSERT INTO uploadedvideos(id,name,url) VALUES('','$name','$url')");
echo "</br>" . $name . " uploaded";
}
?>
Any input?
When a form submits, the browser issues a POST request. When you refresh, the browser issues the last request, thus submitting your form again. However, most browsers will ask you before refreshing after submitting a form. In order to avoid this, you should redirect after a POST.
This is correct functionality. Hitting refresh will re-submit the form and all data with it.
First of all stop using mysql_* functions since they've been deprecated, instead start using prepared statements PDO or MySQLi.
1 way of doing this (my preferred way):
You need some kind of token which is random/unique and you need to save it in table for form submit.
have hidden input field in your form like this:
<input type="hidden" value="<?=md5(time())?>" name="my_form_token" />
before updating table you must check using select statement if given token already exists in table. If it exists do not update database. if it doest exist then update the table with token and your file.
2nd way of doing this is to redirect after submitting to some other page.