$_POST variable empty despite javascript autofill - javascript

I have a html form, that is filled out with javascript when a button is clicked. In php, I would like to get the $_POST variable, but it is empty dispite setting the element.value to something (obviously after form submission).
THE FIELD ACTUALLY CHANGES TO THE DESIRED VALUE, BUT THE $_POST is still empty.
Html:
<form enctype="multipart/form-data" action="upload_file.php" method="post">
...
<input name="price" id="price" type="text"">
...
</form> ...
<span style="float:left"><a onclick="fill_again()" class="button alt
small">Get last values</a></span>
js:
function fill_again(){
document.getElementById("price").value =5800;
}
php:
echo $_POST["price"]; //<--this should be 5800, but it is ""

I have found the solution to my own problem.
I have a js before that modifies the same element:
reNewElement2 = ';'
document.getElementById("size").innerHTML=reNewElement2;
And I don't know why, but even though this is executed prior to the above script, the value remains as declared here, i.e. there is no value.

Related

How to write input value to a file in PHP?

i am quite a beginner in PHP and i wanted to create a input: If you click a button, Javascript will submit it and add linebreak (\n) at the end of what you wrote in the input box, and PHP will write the final result into a file called result.txt. However, when checking result.txt, there is nothing written at all.
HTML
<input id="test" type="text">
<form id="formie" action="test.php" method="post">
<input id="testz" type="text" name="really" hidden="hidden" disabled="disabled"><br>
<button onclick="trigger1()">Submit</button>
</form>
<!-- 2 input boxes, the hidden one is meant to be set to have the final result and with it i do the request -->
<script>
var int;
function trigger1() {
int = document.getElementById('test').value;
document.getElementById('testz').value = int + "\n";
document.getElementById('formie').submit();
}
</script>
PHP
<?php
$postreq = $_POST["really"];
$finaldata = $postreq;
file_put_contents("result.txt", $finaldata, FILE_APPEND);
header("Location: https://domain.tld/test.html");
exit;
?>
No error when stopping the redirect (domain.tld/test.html) and no error in server logs. Why is this happening? I even tried to check this with jshint.com and phpcodechecker.com and both told me no errors
Credit to #user3783243: The problem lies here:
<input id="testz" type="text" name="really" hidden="hidden" disabled="disabled">
Disabled elements cannot have values
Working code:
<input id="testz" type="text" name="really" hidden="hidden">
To answer the question, elements with the disabled attribute are not submitted or you can say their values are not posted
Try removing the "disabled" attribute from your hidden input.
Refer to: Disabled form inputs do not appear in the request, which has reference to the w3 specs explaining that as well.

Javascript function to submit parametric form

I have many forms in a webpage.
Form id is given by php like:
<FORM method="post" id="<?php echo $idform;?>">
In every form the submit button is:
<input name="inoltro" value="Book" type="button" onclick="mySubmit(<?php echo $idform;?>)">
Submit function is:
function mySubmit(FormToSubmit)
{
show_message();
document.getElementById(FormToSubmit).submit();
}
Console javascritp error says it cannot submit null form.
Where am I wrong?
many thanks for your kind help
You can get form object in on* events by passing this.form as a parameter.
Just like that:
function mySubmit(form) {
console.log(form);
}
<form method="post" action="?">
<input type="text" name="test">
<input type="button" name="inoltro" value="Book" onclick="mySubmit(this.form)">
</form>
Press Ctrl+Shift+J in Chrome to see developer's console.
The form you are trying to submit does not exist ...
please check again you are echoing correct values in id attribute of the form ...
onclick="mySubmit('<?php echo $idform;?>')"
make sure this code is outputting something ... use google chrome's inspect element to check the echoed values

Pass javascript variable value to .php using a html form

I have a html form, a variable and a .php script. I would like to pass the value of this variable to the .php script using a html form, it has to be the variables value, so lets say it is 1 then it passes the value 1, but then if the value of the variable changes to 2 then the value the form passes should be 2. Currently when passing the variable, it just passes the name of it.
html:
<form id="get-form" action="getAmount.php" method="POST" name="get-form">
<input type="text" name="amount" id="amount" />
<input type="hidden" id="testVar" name="testVar" value="testVar"/>
<input type="image" src="butImg.png" id="Button" value="pay" alt="but"/>
</form>
<script type="text/javascript">
var testVar= 1;
document.getElementById("testVar").value = testVar;
</script>
php:
$testVar= $_POST['testVar'];
I think the problem is that javascript in your browser is turned off,
so the javascript code will not be executed and the value of testVar will stay the same.
You can easily check that by opening the web inspector of your browser and check if the value of the hidden field is your given value or testVar.
Instead of trying passing the Javascript variable do pass the PHP one
<input type="hidden" name="testVar" id="testVar" value="<?php echo $_POST["testVar"];?>" />
If you need the JavaScript variable for something else you can still have the JS assignment part:
<script type="text/javascript">
var testVar= <?php echo $_POST["testVar"];?>;
</script>

Calling PHP function from Javascript then change form action

I'm trying to change my form action in my HTML then submit using javascript.
The conditions are in PHP .
I need help if anyone can assist me.
This is my PHP function :-
<?php
error_reporting(0);
if(isset($_POST['email'])){
$email=$_POST['email'];
if(!empty($email)) {
$chrono = 0;
} else {
$chrono = 1;
}
}
?>
The motive of the PHP is to check null email entry.
Here's the javascript function :-
<script type="text/javascript">
function fireform(val){
// missing codes
document.forms["demoform"].action=val;
document.forms["demoform"].submit();
}
// missing codes
</script>
HTML :-
<form name="demoform" action="">
<input type ="text" name="name" id="name">
<input type="hidden" name="buttonpressed" id="buttonpressed">
<input type="button" value="submit B" onclick="fireform('b')">
I want to do it in a way , when the user entered an empty email , the PHP will read it as chrono = 0.
Then goes to javascript , if the chrono equal to 0 , the action will remain empty.
If the chrono = 1 , the javascript will change the action of the form and submit.
I need help thanks.
Your flow is unclear: it seems that you want to change the form action from PHP, but PHP is triggered after the form submission. So there's something weird in your flow. You also don't seem to have a field called email in your markup. Add it (or rename the field name):
<input type="text" name="email" id="email">
Nonetheless, having an empty action means the form will be submitted to the page itself.
Probably what you need is a client side validation of the email field. In the fireform() JavaScript function, just add a check for email field:
function fireform(val){
if (document.forms["demoform"].email.value.length > 0){
document.forms["demoform"].action = val;
document.forms["demoform"].submit();
}
}
This should be enough to get what you need.
I would recommend checking the email field (for being empty) in javascript, and when you have set the proper action submit the form in javascript.
Check the field:
$('#<enter id of field>').val();
Update the action:
$('form').attr('action', 'Enter your updatet action here');
Submit the form:
http://api.jquery.com/submit/

Variable Transfer: Web Form that connects with PHP to Database

Hello and thank you for viewing my question. I am a complete beginner and am looking for simple ways to do the following...
What I have in seperate linked documents:
HTML, CSS, Javascript, PHP
What I am having trouble with:
I need to use something like JSON (although I would also accept XML requests or Ajax at this point if they work) to transfer variables from Javascript to PHP. I need the variables to search in a database, so they need to be literally available within PHP (not only seen on a pop-up message or something).
I have seen a LOT of different ways to do this, I have even watched tutorials on YouTube, but nothing has worked for me yet. The things I am having the biggest problem with is that when I add a submit button to my form it doesn't submit my form and I don't know why.
Form code snippet:
<form id="form" name="input" method="post" action="javascript:proofLength();">
<input id="userinput" type="text" autofocus />
<input id="submit" type="button" value="submit" onsubmit="post();">
</form>
The second to last line there doesn't work. Do I need javascript to submit the form? Because I really thought that in this case it was part of the functionality of the form just like method="post"...
The other thing is that for JSON, I have no idea what to do because my variables are determined by user input. Therefore, I cannot define them myself. They are only defined by document.getElement... and that doesn't fit the syntax of JSON.
Those are really my main problems at the moment. So if anyone could show me a simple way to get this variable transfer done, that would be amazing.
After this I will need to search/compare in my database with some php/sql (it's already connecting fine), and I need to be able to return information back to a in HTML based on what I find to be true. I saw one example, but I am not sure that was very applicable to what I am doing, so if you are able to explain how to do that, that would be great also.
Thank you very, very much.
April
You don't need ajax to submit this form. You don't even need javscript. Just do this:
<form id="form" name="input" method="post" action="mytarget.php">
<input id="userinput" name="userinput" type="text" autofocus />
<input id="submit" type="submit" value="submit" />
</form>
This will send the form data to mytarget.php (can be changed of course)
See that i have added the name attribute to your text-field in the form and i changed the type of the button to submit.
Now you can work the Data in mytarget.php like this:
<?
$username = $_POST['userinput'];
echo "Your name is: ".$username;
?>
You wanted to have a check for length in the submit. There are two ways to this:
Before the input is send (the server is not bothered)
Let the server Check the input
for 1 you will have to append a event listener, like this:
var form = document.getElementById("form");
form.addEventListener("submit", function(event){
console.log("test");
var name = form.elements['userinput'].value;
if(name.length < 3){
alert("boy your name is short!");
event.preventDefault();
}
});
Enter a name with less then 3 characters and the form will not be submitted. test here: http://jsfiddle.net/NicoO/c47cr/
Test it Serverside
In your mytarget.php:
<?
$username = $_POST['userinput'];
if(strlen($username) > 3)
echo "Your name is: ".$username;
else
echo "your name was too short!";
?>
You may also do all this with ajax. You will find a lot of good content here. But I'd recommend a framework like jQuery to do so.
The problem is in this line
<form id="form" name="input" method="post" action="javascript:proofLength();">
The action should be a PHP page (or any other type of server script) that will process the form.
Or the proofLength function must call submit() on the form
In the php page you can obtain variable values using $_GET["name"] or $_POST["name"]
To summarize; your code should look like this
<form id="form" name="input" method="post" action="yourpage.php">
<input id="userinput" type="text" autofocus />
<input id="submit" type="button" value="submit">
</form>
and for your php page:
<?php
$userinput = $_POST["userinput"];
//Do what ever you need here
?>
If you want to do something in your javascript before submitting the form, refer to this answer

Categories

Resources