Can I pass javascript variable to a PHP form? - php

I'm using jcrop to crop my photos and need to send the current values along with my form submission. Is there an easy way to do this?
The JavaScript variable is this:
c = coords;
$.param(c)
Can I include this as a hidden field in my PHP form somehow?
<input type="hidden" name="coords" value=" ??? " />

Of course you can:
<form onsubmit='document.getElementById("your-hidden-field-id").value = c.toString(); return true;'>
...
<input type="hidden" name="coords" id="your-hidden-field-id"/>
</form>

Related

Add data to $_POST and submit to another page

I'm trying to add a value to $_POST data while it gets submitted to the target page as follows:
post.php
<?php $_POST['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
catch.php
<?php
foreach ($_POST as $key => $value) {
echo $key . " : ". $value;
echo "<br/>";
}
?>
but I cannot catch 'field1' on the other end. I don't want to use a hidden input field. How can I achieve that?
Thanks!
When you send the form, the $_POST data is reset and assumes only the inputs inside the form and a possible query string you may have appended to form action.
The best way to accomplish what you want is using hidden field but since you dont want it, you can append a query string to your form action:
<form method="post" action="catch.php?field1=Value1">
You're not submitting field1 anywhere. What happens is this:
post.php generates a HTML page (one that doesn't contain any reference to field1)
the user's browser renders the page
on submit, only the elements inside the form are submitted
catch.php receives the elements submitted above.
In other words, you need to get that value into your form:
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input name="field1" type="hidden" value="<?php echo htmlspecialchars($_POST['field1']) ?>"/>
<input type="submit" value="Submit" />
</form>
There is no other way to get the value into your POST data, if it's not present in the form. What you could do as a workaround is store the data in GET (size limit), session (concurrency issues - what happens when the user has two tabs open, each with different session data?), or cookies (size limit AND concurrency issues).
You can't do it this way. If you want to send the data you're trying to add to the POST there only through the users form, you are forced to also store it somewhere client side (e.g. a hidden field or a cookie). What you're doing right now is setting some value into the POST variable, but it gets overridden by the users form post (or rather the $_POST variable you're using after the form post is another instance).
What you could do instead to keep it serverside is save the value in the variable to the session of the user, then in the form post action server side get the value again (given the fact that you're using sessions). Lastly you could just store it in some table in a database, though I wouldn't do this.
Since $_POST are data sent by post method to script, you can not use it for another request directly. You need to compose and send another post request. The easiest way for you will be to use hidden input field/s.
Or you can choose another approach to make http post request, for example curl methods.
If you don't need data to be given by post method, you can save it in session, for example.
try this:
in post.php
<?php $_SESSION['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
in catch.php
<?php
if(isset($_SESSION['field1']))
{
$_POST['field1'] = $_SESSION['field1'];
unset($_SESSION['field1']);
}
foreach ($_POST as $key => $value) {
echo $key . " : ". $value;
echo "<br/>";
}
?>
Make sure you have started the session.
Note: you must use hidden elements or query string as other user suggested.

How to send a part of the form with GET, another with POST?

I'm echoing out a form that uses the method GET to send material via several input type="hidden". Like this:
echo '
<form action="somewhere.php" method="GET" name="whatever" id="whatever">
<input type="hidden" name="get_method" value=" . '$foo' . ">
<input type="hidden" name="want_to_send_with_post" value=" . '$my_post' . ">
</form>
';
This works fine, but how do I send one of these input types as POST, since it's the form that determines the method? Is there a way that an individual input type can override the form method? I know that an input button can override the form method (see here), but that's creating a separate entity. I want to send both GET and Post simultaneously.
The reason: I want to send category names with Get and a text message with POST.
Yeah, you can! Specify the GET data in the URL you're heading to.
(I would edit the GET info with javascript as the user inputs their data)
<input type="text" id="get"> <!--user types get stuff here-->
<form action="somewhere.php?get=something" method="POST" id="form">
<input type="hidden" name="post" value="<?= $mypost ?>" />
</form>
Use JS to change the GET action
var form = doc.getId("form")
var get = doc.getId("get").onkeyup = updateLoc;
function updateLoc() {
form.action = "somewhere.php?get=" + get.value;
}
Then PHP can deal with $_POST["post"] and $_GET["get"] separately.

How can I get/extract the name value of a submit button?

When I create i form - I do something like this:
<form name="form-name" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
[...some elements...]
<input type="submit" name="form-name" value="button">
</form>
Now I need to get the value of the name="" of the submit button, and not the actual value="".
In this case : "form-name".
And here's why:
When I submit a form; I write the action to database - and therefor need the name of the form submitted.
I know I can just have a hidden field with the form name. But I would like to make it simpler by just extracting the name from the submit button because I have a couple of other hidden form elements that I need to add on every single form I create to make my template system work.
And no javascript...
So, let's say your HTML form is this:
<form name="form-name" method="post" action="">
<input type="submit" name="form-name" value="button">
</form>
And you want to get what is inside name="form-name" in this case the form-name
Well, then in the PHP side you can, treat the $_POST global as associative array, and extract the key from it like this:
<?php
if(isset($_POST)){
foreach($_POST as $key=>$each){
echo $key; // this will output "form-name"
}
}
I might have come up with a solution to my question...
Here's a example form:
<form name="vehicle-vinNr" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
<input type="hidden" name="hello" value="world" readonly>
<input type="text" name="element">
<input type="submit" name="vehicle-vinNr" value="send">
</form>
First I need to extract and place the element-names into a new array:
<?php
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
$_FORM_ELEMENT_names[] = $_FORM_ELEMENT_name;
}
}
?>
In this case the array now contains:
hello
element
vehicle-vinNr
If the submit-button is, and always is, the last element in the form - this would work:
$_FORM_name = end($_FORM_ELEMENT_names); // vehicle-vinNr
But sometimes the submit-button is not the last element, so I needed to make a change:
If I always start the name of the submit-button with submit_ - e.g. submit__vehicle-vinNr or with multiple submit buttons for different actions like submit_update__vehicle-vinNr/submit_delete_vehicle-vinNr I can just do this:
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
if(strstr($_FORM_ELEMENT_name,'submit_')){
$_FORM_ELEMENT_submit_name = explode('__',$_FORM_ELEMENT_name);
$_FORM_name = $_FORM_ELEMENT_submit_name[1]; // vehicle-vinNr
}
}
}
This is the solution I came up with - any thoughts?

Submitting a form without any variable names

I need to submit a form to the URL /search/, without any variable names. So that if I input into the form 'foobar', It will then submit the form in GET format to /search/foobar.
How can I achieve this? As far as I can see there is no way to do it with HTML, i'll have to use jQuery.
<form action = "/search/" onsubmit="this.action += encodeURIComponent(this.term.value); this.term.disabled = 'disabled'">
<input type="text" name="term">
</form>

PHP - Accessing SPAN values

I'm new to PHP but I'm working on an email script for an order form.
I have all the values and what not in a form, with a text element that is span-ned for javascript access client side.
What I need to do is also access these span values when I POST.
HTML:
<form name="myform" action="submit.php" method="post">
<strong>Price:</strong> $<span id="SMP">11.99</span>
<strong>Price:</strong> $<span id="SDP">11.99</span>
<strong>Price:</strong> $<span id="YTP">11.99</span>
<strong>Price:</strong> $<span id="SDP">11.99</span>
//bunch of input form code i truncated for readabilty
</form>
You can't do that. The POST array will only contain what you post via input fields. Easiest thing for you to do would be to have some hidden inputs with your values in them
<input type="hidden" name="prices[0]" value ="11.99">
<input type="hidden" name="prices[1]" value ="11.99">
<input type="hidden" name="prices[2]" value ="11.99">
This will be available in your $_POST array. You can access it like
$value1 = $_POST['prices'][0];
Or just iterate it through as an array
You can't. You'll have to add some hidden inputs, and modify them from Javascript, to get them server-side. PHP can't access to the HTML, and only inputs elements are posted.

Categories