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>
Related
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.
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.
I am using window.open() method to open a new window and passing a Javascript variable (String) using localStorage.setItem() method.
In the new window that's opened, I retrive the passed variable using localStorage.getItem() method.
Now I need this variable for an SQL query using PHP,
something like "SELECT rowname from tablename WHERE id = TheJSVariable"
But I couldn't find any where a solution for it.
Any suggestions?
Thanks
When opening with window.open you can add your parameter as common get parameter. i.e.:
window.open ("mysite.com/page.php?param=123");
And then at page.php you can read param value with:
$param = $_GET['param'];
update:
If you want to make post request you could add (hidden) form with form fields holding the values you want to pass and submit that form from code. Something like:
<form id="TheForm" method="post" action="test.asp" target="TheWindow">
<input type="hidden" name="something" value="something" />
<input type="hidden" name="more" value="something" />
<input type="hidden" name="other" value="something" />
</form>
<script type="text/javascript">
window.open('', 'TheWindow');
document.getElementById('TheForm').submit();
</script>
Window.open and pass parameters by post method
I would like to be able to take data stored in a cookie and insert it into a hidden form value.
I am storing the data in a cookie using the following:
<script type="text/javascript">
$(function () {
$("#btnCookie").bind("click", function () {
$.cookie("name", $("#txtName").val());
I am able to retrieve the value as plain text on the next page using:
$.cookie("name")
I have a form on the 2nd page that I want this value to be a part of. How can I insert it into the following:
<input type="hidden" name="cookie" />
I have tried this but it doesn't work:
<input type="hidden" name="cookie" value="$.cookie("name")" />
You can do it like this,
var cookie_val = $.cookie("name");
$('input[name=cookie]').val(cookie_val);
<input type="hidden" name="cookie" value="$.cookie('name')" />
The double quotes are causing an error in the HTML.
Try this jquery cookie u can use in php.
value="<?php echo $_COOKIE['name']; ?>"
or use jquery
$('input[name=cookie]').val($.cookie("name"));
I'm trying to run a JS method with a custom text variable as a parameter. I need to be able to write some text in a form, and then send that value to the method to execute it. I'm not sure why it's not working - it seems to be receiving the value of the VALUE as "" or blank. How would I go about doing this?
<FORM NAME="myform" ACTION="" METHOD="GET">
Choose a Place: <INPUT TYPE="text" NAME="inputbox" VALUE="" id = "place"><P>
</FORM>
<button type="button" onclick="buttonGenerator()">Generate Postcard</button>
<script>
var x = document.getElementById('place').value;
function buttonGenerator(){
generate(x);
}
</script>
Your x variable is being set when the script first runs (when the page is loading). You want to avoid setting it until the button is clicked. Simply move it into the function and you should be set:
function buttonGenerator(){
var x = document.getElementById('place').value;
generate(x);
}