I want to post a php variable from a form - php

I have a php variable "echo $id". Now I want to use the $_POST method to post the variable. I just want to know how to do this for a variable because $_POST[$id] does not work?

I think you are misunderstanding a basic concept here.
The $_POST super global is used to receive input (in the form of a POST request) from the user. While it is possible to set variables in it, you shouldn't.

Your question does not make sense. If you have an HTML form:
<form action="" method="post">
<input type="text" name="something" />
<input type="submit" value="Submit" />
</form>
Then you get the variable $_POST['something'] with whatever the user typed in the text box.
On its own, $_POST is just a variable like any other. You can assign to it $_POST['test'] = 123;, you can delete from it unset($_POST['test']);, you can even make it something other than an array $_POST = "Hello, world";, it just happens to be pre-populated with form data, if any.

With the method $_POST you must be posting to something.
My suggestion to you is to create a form, then have the form going to the file you wish to post to:
So something like this:
echo '<form action = "fileToPostTo.php" method = "post">
<input type = "text" hidden value = "'.$id.'" />
</form>';
And then submit the form when the document loads through jquery or javascript.

You can do it by $_POST['id'] = $id (then You will have it in $_POST['id'] variable (but You shouldn't do it :P).
Or You can send $id by form. Like example:
<form action="/pageToPOST.php" method="post">
<input type="text" value="<?=$id ?>" name="id" />
<input type="submit" name="" value="submit it!" />
</form>
And You'll have $_POST['id'] on http://yourdomainname.com/pageToPOST.php page

you can get and pass the value without page load and form.
<input type="text" name="something" id="something" />
<input type="button" value="ok" onclick="value();"/>
function value()
{
var something=$("#something").val();
var dataparam="oper=show&something="+something;
$.ajax({
type:"post",
url:"yourphpname.pnp",//this is very important.
data:dataparam,
success:function(data)
{
alert(data);
}
});
}
$oper =(isset( $_REQUEST['oper'])) ? $_REQUEST['oper'] : '';
if($oper == "show" and $oper != '')
{
$something=$_REQUEST['something']
echo $something;
}

what you want to do is assign a value submitted to your script using the POST method, to your $id variable. Something like:
$id = $_POST['id'];

Related

How to Automatically POST Form if Variable is passed via URL

I want form to post automatically if zip variable is passed from URL.
URL looks like: www.sitename.com/maps/zipsearch.php?zip=90210
Form looks like:
<form method="post">
Zipcode:
<input name="zip" value="<?php echo (isset($_GET["zip"]))? $_GET["zip"]:"";?>" />
<input type="submit" name="subbut" value="Find instructors" />
</form>
So it fills the input box with zip code but I would like it to post automatically to see results again if zip is passed.
Maybe an IF / THEN?
Any help would be appreciated.
You mean to echo the value passed in GET parameter?
<input type="submit" name="subbut" value="<?php echo isset($_GET['zip'])?$_GET['zip']:'Find'; ?>" />
EDIT
Or, if you are asking about submitting the form, then something like this might work I believe:
<input type="submit" name="subbut" value="<?php echo isset($_GET['zip'])?$_GET['zip']:'Find'; ?>" />
<?php if( isset( $_GET['zip'] ) ) { ?>
<script>
document.forms["name_of_the_form_here"].submit();
</script>
<?php } ?>
like this:
<form id="form" action="form.php" method="post">
Zipcode:
<input name="zip" value="<?php echo (isset($_GET["zip"]))? $_GET["zip"]:"";?>" />
<input type="submit" name="subbut" value="Find instructors" />
</form>
<?php if (isset($_GET["zip"])): ?>
<script>document.getElementById('form').submit()</script>
<?php endif; ?>
since passing data via URL means GET method, so i think you have a little misconception with your question.
if you would like to post automatically you dont need to show form.
just put this code in your zipsearch.php
if ($_GET['zip'] != ""){
// do what you want if zip parameter is not null
}else{
// do what you want if zip parameter is null
}
It looks like your form is submitting to itself. (Eg. zipsearch.php displays HTML form. When user submits form, it is posted back to zipsearch.php which displays the search results).
If this is the case, you don't have to post anything, because you are already inside the file that handles the form submission. You could do something like this:
<?php
if (isset ($_POST['zip'])) {
$zip = $_POST['zip']; /* Form was submitted */
} else if (isset ($_GET['zip'])) {
$zip = $_GET['zip']; /* "?zip=" parameter exists */
}
if (isset ($zip)) {
/* Display search results */
} else {
/* Display form */
}

Parameter appears in URL only after second button click

I have strange problem with my search form. After I enter keyword and do the search request I get empty parameter value.
For example I type in the search field the word "something"
I see an empty value:
search.php?keyword=
After this I enter the keyword "else" and I recieve:
search.php?keyword=something instead of search.php?keyword=else
They somehow appear with "one step back"
I was trying to debug with print_r and var_dump but I only can print some values that does not explain my problem.
Am I missing something very trivial?
Here is what I have:
My class function:
public function show_search_result() {
$this->search_keywords = strip_tags($_GET['keyword']);
$this->_db->query("SELECT * from posts WHERE post_title LIKE '%$this->search_keywords%' OR post_content LIKE '%$this->search_keywords%' LIMIT 100");
$this->rows_results_found = $this->_db->resultset();
}
And my form:
<form action="search.php?keyword=<?php
if (isset($search_results->rows_results_found) && isset($_POST['search_requested'])) {
print strip_tags($_POST['search_keywords']);
}
?>" method="post">
<input type="hidden" name="search_requested">
<input type="text" name="search_keywords" value="<?php
if (isset($search_results->rows_results_found) && isset($_POST['search_requested'])) {
print strip_tags($_POST['search_keywords']);
}
?>"><input type="submit" value="Search">
</form>
<form action="" method=get>
<input type=text id=se>
<?php
if($_GET != null){
$sekw = $_GET ['se'];
$sql = //the query like='$sekw' limit=100;
?>
<input type=submit>
</form>
A simple code.
Your problem is when you send the form, it does save the keywords until the second send.
change your method from post to get. also i would advice you to use a framework for easy and fast coding. some include symfony2, laravel

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?

Pass value in joomla.submitbutton

I have made a submit button using joomla's button like this:
<button onclick="Joomla.submitbutton('autoresponder.transfer')">Edit</button>
I want to pass an 'id' along to autoresponder's transfer function from here. How can i do that?
I tried it using a hidden field like this:
<input type="hidden" name="id" value="<?php echo (int) $item['id']; ?>">
<button onclick="Joomla.submitbutton('autoresponder.transfer')">Edit</button>
But it is not giving me value. Please help!!
1)
instead of <button> you use "submit" like <input type="submit"> .It will work and capture that hidden field with $_POST / $_GET .This usually works for joomla.
Before you submit the form in each handler:
document.getElementById("hiddenId").value = "mySpecialValue";
Like :
<input id="buttonA" type="button" value="do something" onclick="buttonA_clickHandler(event);"/>
function buttonA_clickHandler(event) {
document.getElementById('hiddenId').value = whatever;
document.getElementById('theForm').submit();
}
2)
To pass the value without form submit you can use link .
like :
Do something
I think this will solve your issue.

How to get url id from another page by form action page

i have a page where i am getting the ques id and insert it in the database for that i am doing
url: */faq/faq_question_sol.php?ques= 62*
this ( $selected_ques= ($_GET['ques']); ) is working properly in the *faq_question_sol.php* but the *answer_submit_process.php* does not recognize it
my form
<form id="post-form" class="post-form" method="POST" action="answer_submit_process.php">
<input id="submit-button" type="submit" tabindex="120" name="submitbutton" value="Post Your Answer" />
</form>
and the *answer_submit_process.php* is
if(isset($_POST['submitbutton'])){
$userid = $_SESSION['userid']; // i have already started the session
$selected_ques= ($_GET['ques']);
$content = $_POST["content"] ;
$query="INSERT INTO `formanswer`( `user_id`,`questionid`,`content` ) VALUES ('{$userid}','{$selected_ques}','{$my_html}' ) ";
$result=mysql_query($query);
}
Quickest solution would be saving the value of $_GET['ques'] on a hidden field of the form and thus make it accessible in answer_submit_process.php.
Something like this:
if (isset($_GET['ques'])){
echo '<input type="hidden" name="ques" value="'.$_GET['ques'].'">';
}
And in answer_submit_process page the value could easily accessed by $_POST['ques']..
If you are sending via a form POST, then the variable from which you can get data is $_POST instead of $_GET.
Anyway, i wasn´t able to find any field relating to the ques variable on your form, where are they?
Add <input type="hidden" name="ques" value="<?php echo $_GET['ques'] ?>"/> to your form to temporarily store the variable, and then use the variable $_POST['ques'] in place of $_GET['ques'] in the processing page.
Alternatively, you could change the form action to answer_submit_process.php?ques=<?php echo $_GET['ques']; ?>.

Categories