Form hidden value not being posted - php

I have a form which I want to post to my PHP file. The form consists of the submit button and a hidden value which contains the value of a localStorage entry:
<form action="submit.php" method="post">
<input type="hidden" name="formAnswers" id="localStorageAnswers" />
<input type="submit" id="btnSubmit" value="Submit" />
</form>
jQuery:
$('#btnSubmit').on('click', function() {
$("#localStorageAnswers").val(localStorage.getItem(answers));
});
PHP:
if (isset($_POST['formAnswers'])) {
echo $_POST['formAnswers'];
}
When the PHP file is launched no answers are shown. At first I thought maybe It wouldn't set it on the on('click') but I've set the value at a sooner point on the page with no difference.

I believe answers is not a variable, but a keyword. Use localStorage.getItem("answers") instead of localStorage.getItem(answers)

Related

PHP Read and Write field values without a postback

I want to create a hidden field in my HTML page, all good, just a simple <input> field. Then, I'm trying to read the value of that input field as the page loads the first time, so the closest I've gotten to that is using a $_GET statement. I'm simply trying to echo out the value so that I can see that it's working like such:
<input type="hidden" name="selection" value="fixtures">
<?php
echo $_GET['selection']
and that just gives me nothing, if I try:
echo "<h1>$_GET['selection']</h1>
then I get 0.
Question is - what <form> parameters you have? Did you submit form? Simples example for it will be:
//form.php
<form method="GET" action="next.php">
<input type="hidden" name"selection" value="fixtures" />
<input type="submit" name="submit_btn" value="Send" />
</form>
//next.php
echo '<h1>'.$_GET['selection'].'</h1>';
You can achieve same thing with:
Link

Post variables from js to form and then to php file

first of all, greetings and excuse my English...
I explain my problem:
I have some JS functions that generate some variables, I would like to send these variables via the POST method to a php file to be consulted in a database...
I have read that the best way to do this is sending the variables to the respective values ​​about inputs within a form, Below I show the variables and the form:
Javascript variables:
On click in "Buscar"
$('#Buscar').on('click', function () {
document.getElementById("tipo").value=TipoDeInmuebleDATA.selectedData.value;
document.getElementById("operacion").value=TipoDeOperacionDATA.selectedData.value;
document.getElementById("habitaciones").value=HabitacionesDATA.selectedData.value;
document.getElementById("MetrosCuadrados").value=MetrosCuadrados;
document.getElementById("banos").value=BanosDATA.selectedData.value;
document.getElementById("precio").value=Precio;
});
The Form:
<form name="FormBuscar" method="post" action="consulta.php">
<input id="tipo" name="tipo" type="hidden" />
<input id="operacion" name="operacion" type="hidden" />
<input id="MetrosCuadrados" name="MetrosCuadrados" type="hidden" />
<input id="habitaciones" name="habitaciones" type="hidden" />
<input id="banos" name="banos" type="hidden" />
<input id="precio" name="precio" type="hidden" />
<input type="submit" name="Buscar" class="BotonBuscar">
</form>
I suspect that something is wrong, in the sense that I think the variables are not being sent to consultation
In consultation, if I do the following:
$tipo=$_POST['tipo'];
echo $tipo;
No result :-(
Greetings, I will await your answers in!
The event is wrong. You want to change the values right before submitting. So also add id="buscar-form" to the form tag. Then you can change the jQuery to this:
$('#buscar-form').on('submit', function () {
$('#tipo').val(TipoDeInmuebleDATA.selectedData.value);
$('#operacion').val(TipoDeInmuebleDATA.selectedData.value);
...
});
Forget about the #buscar button. When you click the button, the form is going to submit. So that's the event captured above. Careful with your typing, there are a lot of typos!

Pass One Input Value Into Input Type Hidden Value When User Submits

I have a simple form as outlined in the code below. I would like to append the value submitted in rm_accounts text box to the hidden page_confirm input value at the end of the URL. Hopefully that makes sense.
Essentially, if the user enters '123456' in the rm_accounts textbox, I want value of page_confirm to be http://www.mywebsite.com/index.php?rm_accounts=123456
<form name="signup" method="post" action="https://go.reachmail.net/libraries/form_wizard/process_subscribe.asp" >
<input type='text' name='rm_accounts' value='' />
<input type="hidden" name="page_confirm" value="http://www.mywebsite.com/index.php?rm_accounts=">
<input type="submit" name="Submit" value="Submit" >
</form>
All help is very much appreciated. Thanks!
Use Jquery focusout event to update hidden field value
When user enters 12345 and focus out of textbox or clicks submit(or anywhere) the below code get executed and update the value of hidden field.
$('input[type=text]').focusout(function(){
$('input[type=hidden]').val("http://www.mywebsite.com/index.php?rm_accounts="+$('input[type=text]').val());
console.log($('input[type=hidden]').val());
});
or in submit button click
$('input[type=submit]').click(function(){
$('input[type=hidden]').val("http://www.mywebsite.com/index.php?rm_accounts="+$('input[type=text]').val());
console.log($('input[type=hidden]').val());
});
Working JSFiddle link
http://jsfiddle.net/mkamithkumar/qNdny/1/
I would first suggest you give your HTML some IDs like so:
<form id="signup" method="post" action="https://go.reachmail.net/libraries/form_wizard/process_subscribe.asp" >
<input type='text' name='rm_accounts' id='rm_accounts' value='' />
<input type="hidden" name="page_confirm" id="page_confirm" value="http://www.mywebsite.com/index.php?rm_accounts=">
<input type="submit" name="Submit" value="Submit" >
</form>
Then use some jQuery for your task:
$('#signup').submit(function(){
var value = $('#rm_accounts').val();
var page_confirm = 'http://www.mywebsite.com/index.php?rm_accounts='+value;
$('#page_confirm').val(page_confirm);
});

PHP Post call not returning variable

I have a hidden input field that I fill with some arbitrary value using jquery such that:
function foo() {
// Obtains the contents of a div within the current page
var $elem = $("#something").html();
// Places the contents of the div into a hidden input field
$("#hidden").val($elem);
}
In debugging the thing, I can see that the elem variable obtains the html from within the desired div, but not whether the variable is passed to the hidden input field value.
I have a form:
<form method="POST" action="file.php" target="_blank">
<input id="hidden" type="hidden" value=""/>
<input type="submit" onmouseover="foo()" value="Download"/>
</form>
That upon submission executes the file.php:
<?php
$html = $_POST["hidden"];
echo $html;
?>
The echo call returns nothing, and all I get is a blank page. I am wondering why the value of the hidden input field is not being changed, or why it is not being passed during the POST call.
Some further experimentation revealed that even if I set the value of the hidden field with some random value:
<input id="hidden" type="hidden" value="Some Value"/>
it is still not obtained during the execution of the PHP file. The echo call returns nothing. What is the problem with my obtaining the value of this hidden input field? I have worked very sparingly with PHP but in the past have not had issue with obtaining values from a form upon POST submission.
You need to have a name for your input field in order to access it in php with $_POST
<input id="hidden" name="hidden" type="hidden" value=""/>
You should just indicate name attribute
<form method="POST" action="file.php" target="_blank">
<input name="hidden" id="hidden" type="hidden" value=""/>
<input type="submit" onmouseover="foo()" value="Download"/>
</form>

A second button to start php script, how?

I have read the answer to this question, to execute PHP scripts with the click of a button. But what if I have a "nested button", like this :
<?php
if(!empty($_POST['act'])) {
echo "Ready to rock!";
$someVar = "Rock n Roll";
if(!empty($_POST['act2'])) {
echo $someVar;
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
<?php
}
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="submit" value="Show It!">
</form>
<?php } ?>
I heard my problem can be solved with jQuery, but I no idea.
anyone please.
To execute a script on the server you use the action property of your form:
<form method="POST" action="myscript.php">
When clicking a input type="submit" the browser will go to to action of the form surrounding the input type="submit"
Nesting is not a issue, as the browser always will look for the 'surrounding' form.
Problem is in second form, so it will never calls in this code, because it fails in first $_POST variable IF statement, because in second form you do not POST variable "act". so you need to add it
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
with this form you should see echo $someVar;
p.s. if form action property is emtpy, by default it submits form to the same php script
Just like #DTukans said here, you need the hidden field. If you would post the second form, the value of act will be lost if you are not having a hidden field with the value of act from the first form.
In php you can also check which submit button you submitted by giving the input[type="submit"] a name, such as <input type="submit" name="form2">, then you could check if you submitted that form by:
if (isset($_POST['form2'])) {}, but this is not the case here.
Use the hidden input and you will be good to go.

Categories