Post variables from js to form and then to php file - php

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!

Related

How to send a get form wthout losing get parameters

I have a form located at a url containing get parameters,my form is also using this method.When the form is submitted it rewrites the previos get parameters.
Is there a simple way to rewrite only my form parameters?
I have in mind a Javascript solution ,however I want to know if there is a simpler way?Using HTML/PHP perhaps?
As far as I know, u u are not interested in using JS, then using form's hidden element is only way u have like this-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<input type="hidden" name="country" value="Norway">
<input type="submit" value="Submit">
</form>
<p>Notice that the hidden field above is not shown to a user.</p>
The question is how u can use it with PHP, right?
The solution is here-
//In PHP
if( isset($_GET['fromPerson']) )
{
echo $fromPerson;
}
So combined HTML and PHP code will be like this (assuming a get element from prevous page is named fromPerson)-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<?php
if( isset($_GET['fromPerson']) )
{
echo '<input type="hidden" name="country" value=".$_POST['fromPerson'].">';
}
?>
<input type="submit" value="Submit">
</form>
Lets say you get a parameter p1 from a get request, it should look like this:
http://server.com/?p1=123
In your form, you can add hidden fields that would have the same effect when you submit, like this:
<form method="GET">
<input type="hidden" value="<?php echo $_GET["p1"]; ?>" name="p1">
</form>
That way you can resend the variables as many times as you need.
I'm not sure I understand your question... Can you post your code?
I assume you mean something like this?
in index.php
<input type="hidden" name="id" value="<?php echo $id; ?>" />
in return.php
Edit

Form hidden value not being posted

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)

PHP multiple forms with single action

I've multiple forms with a single action, a single php page that gets called by all the forms.
How can I differentiate which form was sent to the php page?
Using a different unique input type="hidden" for each form.
HTML:
<input type="hidden" name="form_id" value="1">
<input type="hidden" name="form_id" value="2">
PHP:
$myform = $_POST["form_id"];
You can also use the submit button but note that the "value" parameter is what gets displayed to the user so you won't be able to modify it (assuming you want the same text to be displayed on every button).
<input type="submit" name="action" value="the user saw this">
PHP:
$_POST["action"] // -> "the user saw this";
Add a hidden field (action or the like) to each field, then check for it.
<form id="num1">
<input type="hidden" name="action" value="first_action" />
</form>
...and the check:
<?php
if(!empty($_REQUEST['action']) {
switch($_REQUEST['action']) {
case 'first_action':
// first action code
break;
}
}
?>
Give each submitbutton an other name or put a with different values in each form.
You can detect this from the submit button itself too, if submit has different values like:
update name, update profile, delete users...
On the submit button for each form, use different names. Something like:
<input type="submit" name="submit_1" value="Submit" />
<input type="submit" name="submit_2" value="Submit" />
<input type="submit" name="submit_3" value="Submit" />
...
Then on your PHP, you'll have:
$_POST["submit_1"]
$_POST["submit_2"]
$_POST["submit_3"]

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.

Can i Mix $_get and $_post?

in order to edit my entries i want to:
<form id="pregunta" name="pregunta" class="form_pregunta" method="post" action="pregunta.php?id=26">
<h2>Titulo de la pregunta</h2><input name="q" id="q" class="q" value="este es mi títiulo " type="text">
<h2>Describe tu pregunta</h2>
<textarea name="texto" id="texto" style="width: 98%;"><p>esta es mi descripcion</p></textarea>
<h2>Etiquetas</h2>
<input name="tags" id="tags" onmouseover="mostrar_tooltip('nube_e','','0','70','')" onmouseout="ocultar_tooltip('nube_e')" value="dos,tres,una,">
<input name="responde_a" style="display: none;" id="responde_a" value="0">
<button name="pregunta" id="pregunta" type="submit">form_edit_question_button</button>
</form>
And then in file.php
i'd like to $_get['id'] and $_post['inputs']
but when i go:
if(isset($_POST['edit_pregunta'])){
echo 'lalalalalalalalalalalalalalala';
post_edit_pregunta();
}
it won't ever enter :S. is that normal or i'm missing something... i wanted not to have a hidden input with the id of the post i want to edit..
I'm not 100% sure, but forms don't send their name when submitted, much less their id.
You could do the following instead:
<form id="edit_pregunta" method="post" action="file.php?id='$this->id'">
<input type="hidden" name="edit_pregunta" value="anything">
... //inputs here
</form>
and your if should now enter.
It looks like you're checking for your form's "id" attribute. This is not sent when the form is submitted, only values in <input>, <select>, <textarea> and <button> are sent.
You should check for one of those.
Edit: Your button name is "pregunta", so that is the POST variable you should be checking for, eg
if(isset($_POST['pregunta'])){
Just to comment in general on mixing params in the form's "action" and inputs, you can mix them as long as the form method is "post". You cannot set GET params in the form's action and use the "get" method
<!-- Good -->
<form action="proc.php?id=123" method="post">
<input name="foo" value="foo">
<input type="submit">
</form>
<!-- Bad -->
<form action="proc.php?id=123" method="get">
<input name="foo" value="foo">
<input type="submit">
</form>
There should be no problem at all with having get and post variables in the same request, but are you sure your syntax is correct? If this is normal php, shouldn't you write
<form id="edit_pregunta" method="post" action="file.php?id=<?php echo $this->id; ?>">
... //inputs here
</form>
[Edit]
The problem is (if I'm correct and this is standard php) that you generate a form that looks something like this:
<form id="edit_pregunta" method="post" action="file.php?id='$this->id'">
... //inputs here
</form>
This will make id look like this: '$this->id' (including the '-signs). When what you want is something like this:
<form id="edit_pregunta" method="post" action="file.php?id=51">
... //inputs here
</form>
Then $_GET['id'] would be 51.
[Edit2]
Also, I think you need to change
if(isset($_POST['edit_pregunta'])){
with
if(isset($_POST['pregunta'])){
If I'm not mistaken the name of a form doesn't get sent to the server, however, the name of the submit-button does, but I might be wrong about that part.
Yes you can, I've done it several times.
Probably something else is wrong with your code.
Is there any control with name="edit_pregunta" or is it just the id of the form? IDs are not sent to the server.
Simply adding the id to the form will not create the $_POST['edit_pregunta'] you verify.
Instead, inside the form tag, add an <input name="foo" />; in the php script verify $_POST['foo']
While the HTTP spec doesn't disallow query parameters in POST methods, it is somewhat unusual. You'd be better off using a hidden input field in the form to pass any non-user values up to the script.
That said, the syntax for your form is wrong. You need to use "echo" to insert the value of $this->id into the action.
Use input type="submit" in place of button tag.
You need name for form submission and activate php script!
HTML Code:
<form action="change.php" method="POST">
<input type="password" name="p1" class="change_text" placeholder="New Password"/></br>
<input type="password" name="p2" class="change_text" placeholder="Re-Password"/></br>
<input type="submit" name="change" value="Change Password" id="change" />
</form>
PHP Code:
<?php
if (isset($_POST['change']) {
$p1=$_POST['p1'];
}
?>

Categories