I am trying to create the back-end for my app which has to allow emoticons to be send. With a form and php POSTit gets send to a file which sends a notification with the emoticon (and optional text).
However if i use $_POST it will not be be encoded and just send as smiley, which will not be accepted by the devices (apple converts it?)
My Code:
$message = $_POST['message']; // contains only -> š
$test = "š";
echo json_encode($test); //output = "\ud83d\ude07" AS it should be
echo json_encode(strval($message)); //outputs š which is false.
If i use $_GET['message'] and change my other scripts it will work (used test file to check).
However having security in mind (browser history and such) i want to use $_POST.
how can i make this happen?
-- FORM as requested.
Please not that this is all i need So far.
]
<html>
<head>
</head<
<body>
<form action="./simplepush.php" method="post">
Message : <input type="text" name="message" /><br />
<input type="submit" name="submit" value="Send" />
</form>
</body>
</html>
Not using any headers (or specifying)
Have you tried to force the form to encode the post data as UTF8?
<form method="post" action="some/url" accept-charset="UTF-8">
Related
i'm brazilian i does a website simple to sent a simple users data, but to do a test i want sent manually newer data in a link without need complete manual form if it run then i can use directly by my app to save all users data.
see my idea:
mywebsite.com/savedata?method=post&usernamesave=Nome&Misael&userxp=34&userid=35&userlevel=31&usermail=crod%40gmail.com&userprog1=1&userprog2=2&userprog3=23&userprog4=25&userprog5=25&userprog6=25&userprog7=100&userprog8=100&proceed=
if i can change this data from my app and using hrefs i can do this and save a simple data without a complex data connection, it's possible ?
<a href="mywebsite.com/simplepost?method=post&joao&6y"> << type of exeple.
But when i use this and press enterkey the data isn't saved into a textfile, why ?
i using this in php :
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
if ($_SERVER["REQUEST_METHOD"] == "POST") {
/*Php 5.6.2 Code By : Michael S. author*/
//globais
$username = $_POST['usernamesave'];
$userxp = $_POST['userxp'];
$userid = $_POST['userid'];
}
If i got you right you want to save data using a post method. If this is what you want to accomplish, then you don't have to pass the variables in URL. This is the main difference between POST and GET method.
Now for data saving, i will assume that you all ready created a database and a table to save your information on it, so let's jump to the form and how to handle them.
<?php
/*
* First form one will be the POST method.
*/
if(isset($_POST)){
echo "post method where used from a form to send this variables";
var_dump($_POST);
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="username">Username: </label>
<input type="text" name= "username">
<label for="age">Age: </label>
<input type="number" name="age"/>
<input type="submit" value="Submit">
</form>
When you click on submit the information will be sent and can be handled after.
In get method it's different you will see the variables inside the URL after hitting submit, and the same way you can send data to other pages.
<?php
/*
* Second form one will be the GET method
* Check the url.
*/
if(isset($_GET)){
echo "get method where made from a form with this variables";
var_dump($_GET);
}
?>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="username">Username: </label>
<input type="text" name= "username">
<label for="age">Age: </label>
<input type="number" name="age"/>
<input type="submit" value="Submit">
</form>
Now in the url you should see something like
example.com/index.php?username=WaredNsour&age=24
I believe you are using the GET method to send information but in your PHP code, you are using the POST method to fetch them.
Try this :
if ($_SERVER["REQUEST_METHOD"] == "GET") {
/*Php 5.6.2 Code By : Michael S. author*/
//globais
$username = $_GET['usernamesave'];
$userxp = $_GET['userxp'];
$userid = $_GET['userid'];
}
In the code of your page that is processing the variables being sent, try the following (temporarily) as a test to see if variables are being sent/seen. If they are, they will be printed out.
<p>Post vars:
<?php var_dump($_POST) ?>
</p>
If nothing displays, try:
<p>Request vars:
<?php var_dump($_REQUEST) ?>
</p>
Note: in the url you posted you have: &Misael& if Misael is part of the username you should not use the & in front of it. http sees &s as separators for the variables. It will see Misael as a variable name, like: ...&Misael=something&.... If it is a space, use %20.
I have a very basic PHP file. i want to have two textboxes for user input, and a submit button. The user will enter their first and last name, then i would like to append or create a TXT file with the data entered from field1 and field2.
Possibly i am going about this the wrong way. I will post two of the ways i have been tinkering around with.
<html>
<head>
<title>Field1 & 2</title>
</head>
<body>
<form>
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
<?php
$txt= $_POST['field1'].' - '.$_POST['field2'];
$var_str3 = var_export($txt, true); //is this necessary?
$var3 = "$var_str3"; //is this necessary?
file_put_contents('fields.txt', $var3.PHP_EOL, FILE_APPEND);
?>
</body>
</html>
I cant figure out how to get the data from field1 and field2 into a string variable.
I have also messed around with using this php instead of the section listed above
<?php
$txt= "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) {
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
You should learn about HTML Forms And PHP Form Handling.
In your code you have to use a form HTTP method. And the form data must sent for processing to a PHP file.
In this code i use HTTP PSOT method you can also use GET method the result will be same. This two method is used for collecting the form data. And the php file name is "action.php".
index.html
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="action.php" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
action.php
<?php
$path = 'data.txt';
if (isset($_POST['field1']) && isset($_POST['field2'])) {
$fh = fopen($path,"a+");
$string = $_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
Let's take a snippet from http://www.w3schools.com/html/html_forms.asp
<form action="action_page.php" method="post">
First name:<br>
<input type="text" name="firstname" value=""><br>
Last name:<br>
<input type="text" name="lastname" value=""><br><br>
<input type="submit" value="Submit">
</form>
Note the first line: upon form submission a php script is called: action_page.php.
action_page.php is your webpage with the form and the embedded php script. action_page.php both displays the empty form and then process the submitted data.
On the first line also it is specified that the submitted data is sent with the POST method.
The php part will look like this:
<?php
if( isset($_POST['firstname'] ) && isset( $_POST['lastname'] ) )
{
$txt= $_POST['firstname'].' - '.$_POST['lastname'] . PHP_EOL;
file_put_contents('fields.txt', $txt, FILE_APPEND);
}
?>
The if statement is there because the first time the script action_page.php is loaded its purpose is only to display the form and don't receive any POST data.
As the form is submitted by the user the script will receive the data and store to file.
The script will also (with this approach) display again an empty form ready for the submission of another entry.
You can rearrange things in order to have two web pages: one with just the form, another one with a "Thank you" message and the data processing php script.
I have a regular form on my php file, after it's submitted it has to echo a message.
By putting anything in the action="", the only way I can think of displaying a message is by storing it into a session and display it when the page loads if there is a session set.
Everything works fine the way it is right now but w3c validator says I have an error:
Bad value for attribute action on element form: Must be non-empty.
How can I fix this error without having to put # or index.php into the action field?
EDIT:
<form action="" method="post">
<input type="email" name="email" placeholder="Enter your email">
<input type="submit" name="submit" value="SEND">
</form>
<?php
if(isset($_POST['submit']) && isset($_POST['email'])){
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
echo 'This isn\'t a valid email';
}else{
echo 'Great';
}
}
?>
Maintainer of the W3C HTML Checker (validator) here. If your goal is just to get the checker to not emit that error, one way to do that is to go ahead and put # as the value for the action attribute in your HTML source, but then remove it using JavaScript, like this:
<form action="#" method="post">
<script>document.querySelector("form").setAttribute("action", "")</script>
<input type="email" name="email" placeholder="Enter your email">
<input type="submit" name="submit" value="SEND">
</form>
In using the W3C validator https://validator.w3.org/, was presented with the following:
Line 1, Column 1: no document type declaration; will parse without validation
The document type could not be determined, because the document had no correct DOCTYPE declaration. The document does not look like HTML, therefore automatic fallback could not be performed, and the document was only checked against basic markup syntax.
Learn how to add a doctype to your document from our FAQ, or use the validator's Document Type option to validate your document against a specific Document Type.
Along with quite a few more, but I didn't include them here.
The solution:
You need to declare a valid doctype and <head><title> tags, as this will also produce errors if omitted.
Then use action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" as I stated in comments.
Your code will now validate with the following:
<!DOCTYPE html>
<head>
<title>Test page</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<input type="email" name="email" placeholder="Enter your email">
<input type="submit" name="submit" value="SEND">
</form>
</body>
</html>
<?php
if(isset($_POST['submit']) && isset($_POST['email'])){
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
echo 'This isn\'t a valid email';
}else{
echo 'Great';
}
}
?>
Sidenote: You can have the PHP on top also, both methods validated correctly.
Using action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" produced the following/similar in HTML source:
<form action="/folder/file.php" method="post">
While omitting it produced action="" which the W3 validator seems to find invalid and is looking for a valid file for the form's action.
Edit:
In light of the newly accepted answer (mine being unaccepted), do note that your code will not work properly should Javascript be disabled.
I tried with
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
but since in my case $_SERVER["PHP_SELF"] is always index.php (some rules in .htaccess do force this) the solution dosn't work.
In my case I had to use:
<form action="<?=htmlspecialchars($_SERVER["REQUEST_URI"]);?>" method="post">
This return to the same page you was before.
For me the solution was to remove the empty action attribute. The form still works and the validation error is gone.
First time i try to create a simple form using the POST method.Problem is when i click the button nothing gets echoed.
here is my insert.php file :
<?php
if(isset($_POSĪ¤["newitem"])){
echo $itemnew = $_POSĪ¤["newitem"];
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
EDIT: I tried the GET method and it works...Any ideas why that happened? Server configurations?
NEW EDIT: So it turns out i switched method to GET and it worked.Then i switched back to POST (like the code i posted on top) and it works...I have no clue why this happened.Any suggests?
The code you have posted is perfectly valid and should work.
I'm going to guess that you do not have PHP enabled, or it is not working.
<?php ... ?> looks to the browser like a long, malformed HTML tag, and therefore ignores it, making the effect invisible.
Try right-clicking the page and selecting View Source. If you see your PHP there, then the server is indeed not processing it.
The most likely reason for this is probably the same problem I had with my very first bit of PHP code: you're trying to "run" it directly in your browser. This won't work. You need to upload it to a server (or install a server on your computer and call it from there)
Use !empty($_POST['newitem'] instead:
if(!empty($_POSĪ¤["newitem"])){
echo $itemnew = $_POSĪ¤["newitem"];
}
empty()
Try the following:
if($_POST) {
if(!empty($_POST['newitem'])) {
$itemnew = $_POSĪ¤['newitem'];
echo $itemnew;
// or leave it as is: echo $itemnew = $_POSĪ¤['newitem'];
}
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
The if($_POST) will make sure the code is only executed on a post. The empty() function will also check if it isset() but also checks if it is empty or not.
Try this :
<?php
if(isset($_POSĪ¤["newitem"])){
echo $itemnew = $_POSĪ¤["newitem"];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
$_SERVER['PHP_SELF']; is pre-defined variable in php.It allows the user to stay on same page after submitting the form.
Sorry if this is a duplicate, but I really cant find anything that could solve my problem. I can pass numbers and strings like $_SESSION['blabla']="123'; but I can't pass this $_POST value from the textfield and submit button.
Page 1 (sessions.php)
<?php session_start(); ?>
!doctype stuff here
<body>
<form id="form1" name="form1" method="post" action="sessions2.php">
<label>
<input type="text" name="damn" id="damn" />
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</form>
<?php
$omg = $_POST['damn'];
$_SESSION['damn'] = $omg;
echo $_SESSION['damn'] ;
?>
Page 2 (sessions2.php)
<?php
session_start();
$fires = $_SESSION['damn'];
echo "wth";
echo $_SESSION['damn'];
?>
PS. Sorry for the names.. I'm truly stumped.
You need to put the code that reads from $_POST in the file that you submit the form to.
Currently your process is:
Get request for sessions.php
Send form to browser
Assign $_POST['damn'] (which is undefined) to the session.
User submits form
Get request for sessions2.php
Ignore $_POST (which is now populated)
Read from the session (where the variable is still undefined).
damn is populated in the form submission request (step 4/5) not the request where you are trying to read it (step 1).
In sessions2.php
// you POST "damn" variable via form, using post method, so:
$fires = $_POST['damn'];
// and:
$_SESSION['damn'] = $fier;
// or
$_SESSION['damn'] = $_POST['damn'];
PHP code in file sessions.php doesn't work, because in form action you have session2.php.