Pass value in joomla.submitbutton - php

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.

Related

use $_GET['']; value inside $_POST[''] and submit it to the same page and display in the same page

i am new to php and while i am practing i came across a problem. actually,i have two files index1.php and index2.php. in index1.php i have a link with a unique id as
<a href="index2.php?companyid=<?php echo $row('company_id');?>>details</a>
i have got this value in index2.php as
if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
now i have a search form in the index2.php as
<form method="POST" action="index2.php">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
now on button click i want the search results be displayed in the same page as
'index2.php?companyid=$companyid'
but some how if i try to use $_POST['submit']; in the same page it takes me to index2.php and instead of index2.php?companyid=$companyid and also it throws error as undefined index of $companyid if i don't use $_POST['submit']; and echo $companyid; it gives value and works fine. all i want is that to use $companyid' value inside ``$_POST['submit']; as and display the result in the same url as before
if(isset($_POST['submit']){
$companyid //throws an error index of company id
}
any help will be appreciated
First off, it looks like you are not using the company id in the form itself, so it will not be submitted as part of the the POST. You could possibly use:
<form method="POST" action="index2.php">
<?php if (isset($companyid)): ?>
<input type="hidden" name="companyid" value="<?= $companyid; ?>">
<?php endif; ?>
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
But you would probably also need to change your logic to:
if(isset($_POST['companyid'])){
$companyid = $_POST['companyid'];
}else if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
As Josh pointed out in the comments, PHP is not able to remember your previous GET request but this can easily be solved by altering the action attribute of the form element. By doing this you can pass on the previous data. This would look a little something like this:
<form method="POST" action="index2.php?companyid=<?php echo $companyid;?>">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
This way you will be redirected to index2.php with the URL parameters present and you will be able to retrieve both search and companyid using $_POST and $_GET or use $_REQUEST for both.

Send value of submit button when form gets posted

I have a list of names and some buttons with product names. When one of the buttons is clicked the information of the list is sent to a PHP script, but I can't hit the submit button to send its value. How is it done?
I boiled my code down to the following:
The sending page:
<html>
<form action="buy.php" method="post">
<select name="name">
<option>John</option>
<option>Henry</option>
<select>
<input id='submit' type='submit' name='Tea' value='Tea'>
<input id='submit' type='submit' name='Coffee' value='Coffee'>
</form>
</html>
The receiving page: buy.php
<?php
$name = $_POST['name'];
$purchase = $_POST['submit'];
//here some SQL database magic happens
?>
Everything except sending the submit button value works flawlessly.
The button names are not submit, so the php $_POST['submit'] value is not set. As in isset($_POST['submit']) evaluates to false.
<html>
<form action="" method="post">
<input type="hidden" name="action" value="submit" />
<select name="name">
<option>John</option>
<option>Henry</option>
<select>
<!--
make sure all html elements that have an ID are unique and name the buttons submit
-->
<input id="tea-submit" type="submit" name="submit" value="Tea">
<input id="coffee-submit" type="submit" name="submit" value="Coffee">
</form>
</html>
<?php
if (isset($_POST['action'])) {
echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />';
}
?>
Use this instead:
<input id='tea-submit' type='submit' name = 'submit' value = 'Tea'>
<input id='coffee-submit' type='submit' name = 'submit' value = 'Coffee'>
The initial post mentioned buttons. You can also replace the input tags with buttons.
<button type="submit" name="product" value="Tea">Tea</button>
<button type="submit" name="product" value="Coffee">Coffee</button>
The name and value attributes are required to submit the value when the form is submitted (the id attribute is not necessary in this case). The attribute type=submit specifies that clicking on this button causes the form to be submitted.
When the server is handling the submitted form, $_POST['product'] will contain the value "Tea" or "Coffee" depending on which button was clicked.
If you want you can also require the user to confirm before submitting the form (useful when you are implementing a delete button for example).
<button type="submit" name="product" value="Tea" onclick="return confirm('Are you sure you want tea?');">Tea</button>
<button type="submit" name="product" value="Coffee" onclick="return confirm('Are you sure you want coffee?');">Coffee</button>
To start, using the same ID twice is not a good idea. ID's should be unique, if you need to style elements you should use a class to apply CSS instead.
At last, you defined the name of your submit button as Tea and Coffee, but in your PHP you are using submit as index. your index should have been $_POST['Tea'] for example. that would require you to check for it being set as it only sends one , you can do that with isset().
Buy anyway , user4035 just beat me to it , his code will "fix" this for you.
Like the others said, you probably missunderstood the idea of a unique id. All I have to add is, that I do not like the idea of using "value" as the identifying property here, as it may change over time (i.e. if you want to provide multiple languages).
<input id='submit_tea' type='submit' name = 'submit_tea' value = 'Tea' />
<input id='submit_coffee' type='submit' name = 'submit_coffee' value = 'Coffee' />
and in your php script
if( array_key_exists( 'submit_tea', $_POST ) )
{
// handle tea
}
if( array_key_exists( 'submit_coffee', $_POST ) )
{
// handle coffee
}
Additionally, you can add something like if( 'POST' == $_SERVER[ 'REQUEST_METHOD' ] ) if you want to check if data was acctually posted.
You can maintain your html as it is but use this php code
<?php
$name = $_POST['name'];
$purchase1 = $_POST['Tea'];
$purchase2 =$_POST['Coffee'];
?>
You could use something like this to give your button a value:
<?php
if (isset($_POST['submit'])) {
$aSubmitVal = array_keys($_POST['submit'])[0];
echo 'The button value is: ' . $aSubmitVal;
}
?>
<form action="/" method="post">
<input id="someId" type="submit" name="submit[SomeValue]" value="Button name">
</form>
This will give you the string "SomeValue" as a result
https://i.imgur.com/28gr7Uy.gif

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?

Create automatic indivisual link for MYSQL Data

I creating a webpage on mobile price. On the front page i gave the visuals & model of the phone and a "details" button. which submits the id for the phone on a details.php page. It shows the data good without any problem. the link looks like this "www.eeeee.com/details.php".
But i want to show the link as "www.eeee.com/details.php?brand=nokia&id=1111". How can i do this?
Please help me...
If you are using simple form for doing this you can use simple get method instead of post. But if you wanna make your url manually, you need to use form like this.
<form method="get" name="MobileDetails">
<input name="brand" id="brand" value="<?php echo $brand;?>" type="hidden">
<input name="brid" id="brid" value="<?php echo $brandid;?>" type="hidden">
<button type="button" name="submitButton" value="get Details" onclick="getDetails()">
</form>
and javascript for your code will be like
<script type="text/javascript">
function getDetails(){
var brand = document.getElementById('brand').value;
var brandid = document.getElementById('brid').value;
document.MobileDetails.action = 'details.php?brand='+brand+'&id='+brandid;
document.MobileDetails.submit();
}
</script>
you can also do this by using ajax it's totally your choice.

I want to post a php variable from a form

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'];

Categories