I have two files, (file1 and file2). file1 includes file2 in a PHP include statement. File1 also contains a form and prints out all $_POST variables. File2 uses a Javascript button to dynamically change the value in an input field. The problem is that $_POST is empty after submit is pressed. Why is that and how do I fix it?
File1:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php include 'file2.php'; ?>
<input type="submit" /></form>
<?php foreach ($_POST as $key => $val) { echo $key . " belongs to " . $val; } ?>
File2
<script type="text/javascript">
var button = {
counter : 0,
count : function() {
text = document.getElementById("text");
this.counter++;
text.setAttribute("value", this.counter);
}
};
</script>
<button type="button" onclick="button.count()">CLICK ME!</button>
<input id="text" type="text" value="0" />
You forgot to set name attribute:
<input id="text" name="text" type="text" value="0" />
text.setAttribute("value", this.counter);
This is better off as:
text.value = this.counter;
Also you need a name attribute on your element:
<input id="text" name="text" type="text" value="0" />
you didnt set the name attribute for the field "text".
Corrected code:
<input id="text" name="text" type="text" value="0" >
Related
I am trying want to ouput a string ,stored in a variabel, in a label on a page when i click on a button.
But i can't find out how. Still a beginner.
<form action="Test.php" method="post">
Output text: <input type="label" name="word" />
<input type="submit" method="submit" value="Print!" />
</form>
<?php
$word = "test";
if (isset($_POST['submit']))
{
//something that gives the label value $word//
}
?>
There are a few things wrong with your code.
Let me outline them.
Your submit input should have a name attribute, since your conditional statement is based on it if (isset($_POST['submit'])){...}, something I've modified to check if the input is not left empty, using PHP's empty() function.
The input type you have for your "Output text" is invalid, it should be type="text" and not type="label", there is no type="label".
method="submit" for your submit button is invalid for a few reasons. Method belongs in <form> and there is no method="submit".
You then need to assign a POST variable from the input:
such as:
$word = $_POST['word'];
Plus, from what looks to me that you're executing the entire code from within the same page, you can just do action="", unless your code is set in 2 seperate files.
In regards to what you want to achieve: You can then echo the input (if one was entered) using a ternary operator and giving it (the input) a value.
I.e.:
value="<?php echo isset($_POST['word']) ? $_POST['word']: '' ?>"
Here:
<form action="" method="post">
Output text: <input type="text" name="word" value="<?php echo isset($_POST['word']) ? $_POST['word']: '' ?>" />
<input type="submit" name="submit" value="Print!" />
</form>
<?php
if ( isset($_POST['submit']) && !empty($_POST['word']) )
{
$word = $_POST['word'];
echo $word;
}
?>
If you want to use a "label" for your input, then use:
<label for="word">Output text:
<input type="text" name="word" />
</label>
You should also guard against XSS attacks (Cross-side scripting) using:
http://php.net/strip_tags
http://php.net/htmlentities
http://php.net/manual/en/function.htmlspecialchars.php
I.e.:
$word = strip_tags($_POST['word']);
$word = htmlentities($_POST['word']);
$word = htmlspecialchars($_POST['word']);
A few articles you can read on XSS:
http://en.wikipedia.org/wiki/Cross-site_scripting
https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet
Your code should look like this
<form action="Test.php" method="post">
Output text: <input type="label" name="word" value ="<?php echo isset($label['data'])?$label['data']: '' ?>" />
<input type="submit" method="submit" value="Print!" />
</form>
<?php
$word = "test";
$label = array();
if (isset($_POST['submit']))
{
//something that gives the label value $word//
$label['data'] = $word;
}
?>
This should work
Regards
Ahmad rabbani
First of all your input element has type = label, it doesn't mean anything. Change it to type=text
And you are submitting value but not printing it. So in input field you have to print it also.
Look below code.
<?php
$word = "test";
if (isset($_POST['submit']))
{
// whatever you do with $word
}
?>
<form action="Test.php" method="post">
Output text: <input type="text" name="word" value="<?php echo $word; ?>"/>
<input type="submit" name="submit" value="Print!" />
</form>
UPDATE
One more thing I forgot to mention that you are submitting form to Test.php and printing this to file, if this file's name is Test.php then not an issue, other wise leave action property blank, so it submit data to itself.
method = submit there is nothing like this. you can set button name to submit, like name= "submit".
Is it possible?
My question is that :
I have a form having fields like this:
<input type="hidden" id="chargetotal_reload_uk" name="chargetotal" value="12.01" />
<input type="text" id="name" name="name" value="xyz" />
and another form:
<input type="hidden" id="chargetotal_reload_uk" name="chargetotal" value="12.01" />
<input type="text" id="name" name="name" value="xyz" />
In both cases:
print_r($_POST);
give me:
array(
'chargetotal'=>'12.01',
'name'=>'xyz',
);
Is it possible to identify hidden field in $_POST ie chargetotal was the hidden field in the form?
As stated above, you should probably already know the names of your hidden fields, but technically, you could pass the field type in with the name, then explode on the $_POST keys in your script.
HTML
<input type="hidden" id="chargetotal_reload_uk" name="charge total::hidden" value="12.01" />
<input type="text" id="name" name="name" value="xyz" />
PHP
<?php
$vars = array();
foreach($_POST as $key => $val){
if(sibstr_count($key, '::') > 0){
$key = explode('::'. $key);
$vars[$key[0]] = array('fieldType' => $key[1]. 'value' => $val);
} else {
$vars[$key] = $val;
// or $vars[$key] = array('fieldType' => ''. 'value' => $val); if you need to keep the same format
}
}
As per your comment on usage, you can use PHP sessions for this.
You would need to set your value in $_SESSION['value'] = "12.01"; for it to work.
<?php
session_start();
$_SESSION['value'] = "12.01";
?>
<?php
if (isset($_SESSION['value']) || !empty($_SESSION['value'])) {
echo "The value is: " . $_SESSION['value'];
}
else {
echo "<div id='session_name'>No value set.</div>";
}
?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
var thevalue = "<?php echo $_SESSION['value']; ?>";
// alert (thevalue);
});
</script>
<br><br>
<input type="hidden" id="chargetotal_reload_uk" name="chargetotal" value="<?php echo $_SESSION['value']; ?>" />
<div id="session_name">The value is: <?php echo $_SESSION['value']; ?></div>
HTML source/output:
The value is: 12.01
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
var thevalue = "12.01";
// alert (thevalue);
});
</script>
<br><br>
<input type="hidden" id="chargetotal_reload_uk" name="chargetotal" value="12.01" />
<div id="session_name">The value is: 12.01</div>
I have a form with some jQuery UI sortables that have data-attributes that get serialized into a hidden textbox, so when I send the form, my PHP script can look at the value of $_POST["contents"](which is a string like this 1-2-2-1) and pass it to my MySQL UPDATE query. The problem is, the reported value of the textbox before sending the form (from the javascript), is different than the one in $_POST.
The form:
<form method="post" action="?update">
<input type="text" name="id" style="display:none" value="1" />
<input type="text" name="path" placeholder="Endereço Ex.: /pacotes/pureenergy" value="/some/path" /> <input type="text" name="title" placeholder="Título da página" value="<?php echo $t;?>" /><br />
<!-- Some stuff hidden for briefness -->
<input type="text" id="ci" name="contents" style="display:none" value="1-2-2-1" />
<input type="submit" value="Atualizar página" onclick="updateForm()">
</form>
The Javascript:
function updateForm() {
var txt = $("#dropzone li:first-of-type").data("id");
$("#dropzone li:not(:first-of-type)").each(function() {
txt = txt+"-"+$(this).data("id");
});
$("#ci").text(txt);
alert(txt);
}
The PHP:
$id = $_POST["id"];
$p = $_POST["path"];
$t = $_POST["title"];
$c = $_POST["contents"];
mysql_query("UPDATE aruna.pages SET path='$p', title='$t', contents='$c' WHERE id=$id") or die(mysql_error());
The value reported by the javascript alert() is behaving as intended, but when I send the form, the value of the $_POST["contents"] is the same that was hardcoded in the HTML.
EDIT: PHP doesn't complain about $_POST["contents"] being unset, which it would if there was a typo.
You may try .val() instead of .text()
Sorry if this is a rather basic question.
I have a page with an HTML form. The code looks like this:
<form action="submit.php" method="post">
Example value: <input name="example" type="text" />
Example value 2: <input name="example2" type="text" />
<input type="submit" />
</form>
Then in my file submit.php, I have the following:
<?php
$example = $_POST['example'];
$example2 = $_POST['example2'];
echo $example . " " . $example2;
?>
However, I want to eliminate the use of the external file. I want the $_POST variables on the same page. How would I do this?
Put this on a php file:
<?php
if (isset($_POST['submit'])) {
$example = $_POST['example'];
$example2 = $_POST['example2'];
echo $example . " " . $example2;
}
?>
<form action="" method="post">
Example value: <input name="example" type="text" />
Example value 2: <input name="example2" type="text" />
<input name="submit" type="submit" />
</form>
It will execute the whole file as PHP. The first time you open it, $_POST['submit'] won't be set because the form has not been sent.
Once you click on the submit button, it will print the information.
I am learning PHP now, so pardon my silly question which I am not able to resolve.
I have created a simple web form where in I display the values entered by a user.
function submitform()
{
document.forms["myForm"].submit();
} // in head of html
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" onclick="submitForm()" />
</form>
and res.php contains:
foreach($_POST as $field => $value)
{
echo "$field = $value";
}
When I click on the submit button, I just get a blank page without any values from the form. Can anyone please let me know what am I missing?
There's no need for the javascript. This should do:
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" />
</form>
Let's start with fixing errors:
JavaScript is case-sensitive. I see that your function name is submitform and the form's onclick calls submitForm.
The javascript is not really necessary from what you've shown us, I would try this on a single php page and see if it works:
Create a test.php file for test purpose:
<?php
if($_POST){
foreach($_POST as $key=>$value){
echo "$key: $value<br />";
}
}else{
<form action="test.php" method="post">
<input type="text" value="1" name="name1" />
<input type="text" value="2" name="name2" />
<input type="submit" value="submit" name="Submit" />
</form>
}
?>
If it does work, slowly work your way into your current form setup to see what is breaking it. If it doesn't work, there's something larger at play.
There are 2 things you should do now.
Remove the JavaScript function to submit the form. It's not required (or necessary). The default behavior of a submit button is to... well... submit. You don't need to help it with JavaScript.
Enable error display by using error_reporting(E_ALL).
After you do both things, you should be able to debug and assess the problem much more easily.
Put your php code inside php tags!
<?php
foreach($_POST as $field => $value)
{
echo $field ." = ." $value.'<br />';
}
?>
If you do
<?php
print_r($_POST);
?>
what do you get?
If this still doesn't work, does your server parse php?
Create the file test.php and access it directly http://localhost/test.php or whatever your URL is
<?php
echo 'Hello';
?>
if this doesn't work..it's a whole diferent problem
You can submit an HTML form using PHP with fsubmit library.
Example:
require_once 'fsubmit.php';
$html = "<form action ='res.php' method='post'><input type='text' name='name'/></form>";
$form = new Fsubmit();
$form->url = 'http://submit_url_here.com';
$form->html = $html;
$form->params = ['name'=>'kokainom'];
$response = $form->submit();
echo $response['content'];