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" />
}
Related
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!
Anchor tag acts as a form submit button?
<form method="get" action="<?php bloginfo('url'); ?>/">
<div class="field-search">
<input type="text" class="form-control input-lg" placeholder="Search for..." value="<?php the_search_query(); ?>">
<i class="fa fa-search font-16"></i>
</div>
<form action="test.aspx" type="POST">
<label>
<span>Username</span>
<input type="text" name="UserName" id="UserName" class="input-text required" />
</label>
<label>
<span>Password</span>
<input type="password" name="Password" id="Password" class="input-text required" />
</label>
<label>
<input type="checkbox" name="RememberMe" id="RememberMe" class="check-box" />
<span class="checkboxlabel">Remember Me</span>
</label>
<div class="spacer">
Login
</div>
</form>
jquery
$(document).ready(function(){
$(document).on("click",".login-button",function(){
var form = $(this).closest("form");
//console.log(form);
form.submit();
});
});
possible duplicate of
(Anchor tag as submit button?)
(This is a copied answer)
Try this approach see if it works.
Add a click event to your anchor tag as below and do a event.stopPropagation()
$('a').on('click', function(e){
e.stopPropagation();
})`
See if this works
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>
I'm trying to submit a form with zombie.js, and pressButton() is failing with "Error: No BUTTON 'xxx'", where xxx is the selector. Here is the app:
var Browser = require('zombie');
b = new Browser();
b.visit('https://www.example.com/login/', function() {
b.
fill('name', 'My Name').
fill('code', 'My Code').
pressButton('submit', function() {
console.log(b.html());
});
});
And here is the form:
<form method="post" class="login">
<p> <label for="name"> <span id="nameprompt">Your Name:</span> </label> </p>
<input name="name" id="name" value="" size="40" maxlength="40" />
<p> <label for="code"> <span id="codeprompt">Bar Code</span> </label> </p>
<input name="code" id="code" type="PASSWORD" value="" size="40" maxlength="40" />
<div class="formButtonArea">
<input type="image" src="/screens/pat_submit.gif" border="0" name="submit" value="submit" />
</div>
</form>
I've tried a bunch of selectors including things like ".formButtonArea input" in addition to the obvious "submit" with no success.
What am I doing wrong?
It seems like you need the input to be of type="submit" (or type="button" perhaps). According to https://groups.google.com/forum/?fromgroups=#!topic/zombie-js/8L0_Cpn8vVU, where the author comments on clickLink, it's likely that clickButton will do the same: find buttons and then fire click.
Hope it helps.
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.