Can't pass php value in a form's value - php

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.

Related

After Using Get Method , Clean query strings in Adress Bar,

My php code in one page
<?php
if(isset($_POST['gonder'])){....}
if(isset($_GET['ozelid'])){.....}
?>
<input type="submit" name="gonder">
<td> <a href=\"?ozelid=YERTUTUCU&fiyat=$haskur\" onclick=\"return confirm('Tahsil Edilsin?')\" >Tahsil</a>
First time after the page loads ,when I click submit, it is okay and it only runs if(isset($_POST['gonder'])){....}
Again first time after the page loads , when I click the link then it only runs
if(isset($_GET['ozelid'])){.....}
However, after the page loads, when click the link and then I click the submit button both if(isset($_POST['gonder'])){....} and if(isset($_GET['ozelid'])){.....} run, which is not desired.
If you only want one of these being called at a time, then I would suggest just using an ElseIf.
<?php
if(isset($_POST['gonder'])){....}
Elseif(isset($_GET['ozelid'])){.....}
?>
You shoud check your form tag:
If you are using <form action="#" or keep the action attribute empty, the form is sent to the current page (in your case it's the page having the GET parameters).
Please check if you have the possibilty to set the action attribute of your form tag to something like this:
<form action="index.php" method="post">
You have to replace index.php with the name of your php file.
After you processed the request you can send location header. Like so:
header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
die();
To your current url without get parameters. I guess this should be in your code in this block:
if(isset($_GET['ozelid'])){.....}

PHP and html post button value

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.

WordPress Custom Page Submit Form

I'm trying to write a simple script to write some data to mysql then read it. My code is working without a problem alone but I'm trying to use it inside a WordPress page, this is the point problem starts.
I have created a template file for WordPress, and using this template to create the page. Page shows up without a problem but whenever I try to submit the form inside it (my custom php form) it forwards me to index.php .
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<span>Enter Your Code : </span><br/>
<input type="text" name="sha256"><br/>
<p align = right><input type="submit" name="shaSubmit" value="Submit" /></p>
</form>
this is my form (inside custom php), and as you can see it posts the data to itself. At the the start of my custom php code I have
if(isset($_POST['Submit']))
But it doesn't matter, as soon as I click on button, it forward me to domain.com/index.php
Btw, this custom php is on a page with such url domain.com/custompage/
How can make this form work ?
ps. Code above is for reading from mysql.
You're using the following conditional statement if(isset($_POST['Submit'])) along with the submit button's named element name="shaSubmit".
You need to make those match.
Either by changing the name of your submit button to name="Submit"
or by changing your conditional statement to if(isset($_POST['shaSubmit']))
which is why your code is failing because of the conditional statement you've set is relying on a form element named "shaSubmit".
You need to change 2 things.
(1)make action=""
don't need to use action=" echo htmlspecialchars($_SERVER["PHP_SELF"])"
(2)if(isset($_POST['shaSubmit'])){
the code ....
}

PHP File upload external url

I have build a form in which you can add images. Unfortunatelly, I am not able to call "move_uploaded_file" in PHP, as the server is running PHP safe mode and there is no way to modify this (I have checked). Therefore, I submit my form to my OWN server, which handles the file uploading.
On my own server, the file however; is not found. I think it has to do with the form calling the external url.
I know this because
echo $_FILES['uploadFile']['name'] ."<br>";
returns just an empty line.
The form itself is:
<form action="http://ejw.patrickh.nl/load.php" method="get" enctype="multipart/form-data" onsubmit ="return checkInput();"> </form>
and contains several input buttons.
Bottomline: the form on my own server submits the file perfectly, however when I make use of the form which is on another site, with the above action; no file is found.
Can this be fixed, and if so; how?
Thanks in advance!
You have to use method="post" to submit files.
Also the enctype attribute alone can be used only, if method="post".

PHP/MySQL query ignored if 'action' is set in form

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.

Categories