Joomla 3.2 Multiple submit buttons in component - button

iam struggeling with two submit buttons in my component form.
<button type="submit" class="button"><?php echo JText::_('Save1'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="data.save_1" />
<button type="submit" class="button"><?php echo JText::_('Save2'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="data.save_2" />
The problem ist, each button leads to the controllers function save_2.
If iam changing the order to
<button type="submit" class="button"><?php echo JText::_('Save2'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="data.save_2" />
<button type="submit" class="button"><?php echo JText::_('Save1'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="data.save_1" />
both buttons are executing the function save_1 in the controller.
It is always executing only the task of the last button. What is wrong here?
I would like to execute controllers task1 when I use the button task1, and execute controllers task2 when I use the button task2.
thx Perino

Thank you! I did it now on a similar way:
in the view (i worked with different sub-layouts in this view), id did now
<button type="submit" class="button" name="save_1"><?php echo JText::_('Save1'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="data.save" />
<button type="submit" class="button" name="save_2"><?php echo JText::_('Save2'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="data.save" />
Then in the controller i did
public function save()
{
If (Isset($_POST['save_1']))
{
echo "Button 1 saved"; // here you can do your task for button1
}
If (Isset($_POST['save_2']))
{
echo "Button 2 saved"; //here you can do your task for button2
}
I hope this can also help anybody with same problem.

it looks like both your submit-buttons are in the same form. So the last taks - input will overwrite the first. So either you need to make two forms (probably not what you want?) or you need to handle the click-event depending on which submit-button is clicked, and update the task-variable accordingly, something like:
<button type="submit" class="button"><?php echo JText::_('Save2'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" id="formtask" name="task" value="data.save_2" />
<button type="submit" class="button"
onclick="document.getElementById('formtask').value='data.save_1'">
<?php echo JText::_('Save1'); ?></button>

Related

Why this is showing me nothing in Wordpress?

Why this is show me nothing??
I'm totally lost =(
<?php function calc_shortcode(){ ?>
<div>
<form>
<h3>Price High</h3>
<input name="priceHigh" type="text" />
<h3>Price Low</h3>
<input name="priceLow" type="text" />
<h3>Price Up</h3>
<input name="priceUp" type="text" />
<h3>Price Down</h3>
<input name="priceDown" type="text" /></br>
<input name="submit" type="submit" value="Calculate" class="btn btn-primary" />
</form>
</div>
<?php
}
add_shortcode('calc_pat', 'calc_shortcode');
Thanks for helping guys!
To use shortcode you need to call it. Read more here https://developer.wordpress.org/reference/functions/add_shortcode/ and https://developer.wordpress.org/reference/functions/do_shortcode/
echo do_shortcode('[calc_pat]');
The php labels are wrongly placed there, try doing this:
<?php
function calc_shortcode(){
<div>
<form>
<h3>Price High</h3>
<input name="priceHigh" type="text" />
<h3>Price Low</h3>
<input name="priceLow" type="text" />
<h3>Price Up</h3>
<input name="priceUp" type="text" />
<h3>Price Down</h3>
<input name="priceDown" type="text" /></br>
<input name="submit" type="submit" value="Calculate" class="btn btn-primary" />
</form>
</div>
}
add_shortcode('calc_pat', 'calc_shortcode');
?>
Check if that works
Please give try to below code, it should work:
function calc_shortcode(){
echo '<div>
<form>
<h3>Price High</h3>
<input name="priceHigh" type="text" />
<h3>Price Low</h3>
<input name="priceLow" type="text" />
<h3>Price Up</h3>
<input name="priceUp" type="text" />
<h3>Price Down</h3>
<input name="priceDown" type="text" /></br>
<input name="submit" type="submit" value="Calculate" class="btn btn-primary" />
</form>
</div>';
}
add_shortcode('calc_pat', 'calc_shortcode');
you can also add ob_start(); and ob_get_clean();
Try below code :
<?php function calc_shortcode()
{
ob_start();
?>
<div>
<form>
<h3>Price High</h3>
<input name="priceHigh" type="text" />
<h3>Price Low</h3>
<input name="priceLow" type="text" />
<h3>Price Up</h3>
<input name="priceUp" type="text" />
<h3>Price Down</h3>
<input name="priceDown" type="text" /></br>
<input name="submit" type="submit" value="Calculate" class="btn btn-primary" />
</form>
</div>
<?php
echo ob_get_clean();
die();
}
add_shortcode('calc_pat', 'calc_shortcode');

Obtain data from a form in wordpress

I'm building a wordpress website using Avada tamplate. I use the Avada login form in which the users insert their username and password. I would like to get the username of the user when he click on the form button and he's redirect to login.php page. I don't understand why I can't do that.
The code of the form (i removed "class" and "style" in order to reduce it) is:
<form name="loginform" id="loginform" method="post" action="http://www.mywebsite.com/es/wp-login.php">
<div>
<div>
<label for="user_login">Username</label>
<input type="text" name="log" placeholder="Username" value="" size="20" id="user_login" />
</div>
<div>
<label for="user_pass">Password</label>
<input type="password" name="pwd" placeholder="Password" value="" id="user_pass" />
</div>
</div>
<div>
<div>
<button type="submit" name="wp-submit">Log in</button>
<input type="hidden" name="user-cookie" value="1" />
<input type="hidden" name="redirect_to" value="login.php" />
<input type="hidden" name="fusion_login_box" value="true" />
<input type="hidden" name="_wp_http_referer" value="/es/area-privada/" /><span></span></div>
<div></div>
</div>
</form>
The login.php code is very simple:
<?php
$username = $_POST['log'];
echo $username;
?>
however the variable $username is blank and doesn't capture the username. Someone can help me please? thanks!

How can I trigger Html.BeginForm by clicking a button?

Here is my Html.BeginForm code. I want to pass 2 values to the Action CheckOutProduct.
How can I trigger Html.BeginForm by clicking a button?
#Html.BeginForm("CheckOutProduct", "CheckOut")
{
<input type="hidden" id="productCodeForCheckout" value="" />
<input type="hidden" id="productQtyForCheckout" value="0" />
}
<button class="btn" id="checkout"><span>add to cart</span></button>
Just place a submit button inside your form
#Html.BeginForm("CheckOutProduct", "CheckOut")
{
<input type="hidden" id="productCodeForCheckout" value="" />
<input type="hidden" id="productQtyForCheckout" value="0" />
<input type="submit" class="btn" id="checkout" title="Add to Cart" />
}
If you prefer to stick to button instead of input, just set the type to submit
<button type="submit" class="btn" id="checkout">...</button>
You should add a submit button to the form
#Html.BeginForm("CheckOutProduct", "CheckOut")
{
<input type="hidden" id="productCodeForCheckout" value="" />
<input type="hidden" id="productQtyForCheckout" value="0" />
<input type="submit">Add to cart</button>
}
You can also add FormMethod.Post, a HTTP method for processing the form, either in a form of GET or POST, which in your case is POST method. This will make sure that your method in your controller will fire after Post:
#Html.BeginForm("CheckOutProduct", "CheckOut", FormMethod.Post)
{
<input type="hidden" id="productCodeForCheckout" value="" />
<input type="hidden" id="productQtyForCheckout" value="0" />
<input type="submit" class="btn" id="checkout" title="Add to Cart" />
}

WordPress Search on pluign page

I have an plugin to didsplay list of coupon code in WordPress. I would like to place search box for searching coupon code in coupon page.
<form method="get" id="searchform" action="">
<input type="text" value="Search" name="s" id="s" />
<input type="hidden" name="cat" value="5" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
When I click on search button it redirect me to wordpress post page instead of plugin default page.
Suggest me working solution for implement searching in plugin page.
We have to redirect it manually using this form:
<form method="get" action="<?php echo "edit.php?";?>" id="coupon">
<label class="screen-reader-text" for="tag-search-input">Search Coupon Code:</label>
<input type="hidden" name="post_type" value="wpsc-product" /> //required to redirect it coupon page
<input type="hidden" name="page" value="wpsc-edit-coupons" />//required to redirect it coupon page
<input type="search" id="search-input" name="s" value="">
<input type="submit" name="" id="submit" class="button" value="Search Coupon Code">
</form>
After this you will have to grape the result from $_GET['s'] and apply manual search on that file.

Need ASP code for a simple form - radio buttons redirect to urls

Can someone direct me to an example of asp code that will process a form which has only radio buttons? The functionality: When a radio button is checked and submit is clicked the user will be redirected to specific url. onclick="window.location='http://www.google.com';" Will not do the trick here. We have to wait until "submit" is clicked.
<form name="input" action="the_code_you_are_helping_me_with.asp" method="get">
<input type="radio" name="option1" value="1" />option1<br />
<input type="radio" name="option2" value="2" />option2<br />
<input type="radio" name="option3" value="3" />option3<br />
<input type="radio" name="option4" value="4" />option4<br />
<input type="radio" name="option5" value="5" />option5<br />
<input type="submit" value="Submit" />
Thanks in advance,
dp
See a working demo here: http://jsfiddle.net/89s6R/3/
<script>
function onRadioClick(radio) {
radio.form.action = radio.value;
}
function onFormSubmit(form) {
window.location = form.action;
return false;
}
</script>
<form name="input" action="#" method="get" onsubmit="return onFormSubmit(this)">
<label><input type="radio" name="destination" onclick="onRadioClick(this)" value="http://google.com" />Google</label><br />
<label><input type="radio" name="destination" onclick="onRadioClick(this)" value="http://nyt.com" />New York Times</label><br />
<label><input type="radio" name="destination" onclick="onRadioClick(this)" value="http://twitter.com" />Twitter</label><br />
<label><input type="radio" name="destination" onclick="onRadioClick(this)" value="http://microsoft.com" />Microsoft</label><br />
<label><input type="radio" name="destination" onclick="onRadioClick(this)" value="http://apple.com" />Apple</label><br />
<input type="submit" value="Submit" />
</form>​
The above client-side answer will work. However, if you want "asp code" I assume you wanted something server side. So:
Turn your submit button into an asp:Button.
Turn your radio buttons into an asp:RadioButtonList.
In the button's onClick event, get the value of the selected button. Then user Response.Redirect to redirect to that url.

Resources