This question already has answers here:
how can I make onclick function to submit just my form.php
(2 answers)
Closed 9 years ago.
I have this function on an html form, the form script there is this
<script type="text/javascript">
var conf = {
isVip:false,
isMem:false
};
</script>
and here is the the button, the button is to process two form in the html, here is the button.
<li class="current">
免费邮箱登录
</li>
<li>
<span class="vip"></span>VIPSubmit
</li>
<a href="#" class="loginBtn"
Here is the form
<form name="vip_login" method="post" action="process.php">
<input tabindex="1" id="vipname" type="text" class="username" name="username" value=""/><span class="vipDomain">#name.com</span>
<input tabindex="2" id="vippassword" type="password" class="password" name="password" value=""/>
My question is, how do i make the btn to process my form alone and send data to external process.php function.
First, give the form an id:
<form id="vip_login" method="post" action="process.php">
Next, download jQuery if you don't already have it, and then setup the onclick of the href to be this:
<a href='#' class='loginBtn' onclick="$('#vip_login').submit();" ...
and that will force the submit of the form.
This is correct
<form name="vip_login" method="post" action="process.php">
But you need to add a submit button, or use some javascript to do it, like
document.vip_login.submit()
You can add the previous after all validations are ok.
First of all, you have links not buttons. You need to make buttons with IDs.
<li class="current">
<input id="btn1" type="button" value="免费邮箱登录"></input>
</li>
<li>
<input id="btn2" type="button" value="VIPSubmit"><span class="vip"></span></input>
</li>
Second, you can then use jQuery/AJAX to submit your form from clicking these buttons. You will need to add an ID to your form as well. Below, I made the ID the same as the 'name' attribute.
$(document).ready(function() {
$("#btn1").click(function() {
$("#vip_login").submit(function() {
$.ajax({
type: "POST",
data: $(this).serialize(),
cache: false,
url: "process.php",
success: function(data) {
$("#id on process.php").html(data); //If setting in process.php
window.alert("Data send");
}
});
return false;
});
});
});
Also note: Using AJAX, you do not need an "action" supplied in your form.
<form name="vip_login" id="vip_login" method="post" action="">
add an id to your <form> so it would be something like this:
<form id="vip_login" name="vip_login" method="post" action="process.php">
Then set the button's attributes to be like this:
Submit
This will get it to work, although the more elegant way to do it is to add a submit button within this form by adding the following element in your <form>:
<input type="submit" value="Submit!" />
Then if you click on this button the parent form will be submitted.
Related
I have a signup form in my HTML file. The action of the form is a PHP file. I want the PHP to process the entered data and leave the user on the signup page (HTML.) However, when the submit button is clicked, the website redirects to the PHP file. Is there some way of preventing this?
I have tried setting the form's target to "_self", but that didn't help.
HTML:
<? include('signup.php'); ?>
<?php include('errors.php'); ?>
<form method="post" action="signup.php">
***More Input fields here***
<button type="submit" name="submit">Sign Up</button>
you can actually leave that empty, so the same page gets targeted :)
Remove or blank the action attribute in form like as:
HTML:
<?php include('signup.php');
include('errors.php');
?>
<form method="post">
***More Input fields here***
<button type="submit" name="submit">Sign Up</button>
</form>
If you don't want to refresh the page and handle user input you can use AJAX
Simple example:
<form>
<label for="username">
Username
<input type="text" id="username">
</label>
<label for="password">
Password
<input type="password" id="password">
</label>
<button id="submit"></button>
</form>
<script>
$(function () {
$('#submit').click(function () {
$.ajax({
method: 'POST',
url: 'signup.php',
data:
{
name: $('#username').val(),
password: $('#password').val()
}
}).done(function (msg) {
alert('Data Saved: ' + msg);
});
});
});
</script>
If you want to send POST request to the same page, just remove action attribute from the form
<form method="post">
***More Input fields here***
<button type="submit" name="submit">Sign Up</button>
I am using jquery ajax form library.... i want to change action url when i submit form
html
<div class="modal-body form"><?php echo form_open_multipart("#",array('id'=>'mng_form', 'name'=>'mng_form','class'=>'form-horizontal','role'=>'form')); ?>
javascript
var options = {replaceTarget:true, beforeSubmit: showRequest, success: showResponse};
how can i change action url using jquery form ?
use jQuery submit(). This happens prior to the actual submission, so you can change the action url.
<form id="fromID" action="action_page01.php" method="post" target="_blank" accept-charset="UTF-8" enctype="application/x-www-form-urlencoded" autocomplete="off" novalidate>
<input type="submit" value="Submit">
</form>
// jQuery
$( "#fromID" ).submit(function( event ) {
$('#fromID').attr('action', 'action_page02.php');
});
Due to language limitations I might not understood how to ask Google about what I want to accomplish, but I hope you will understand.
I have three forms which i want to show on the same page without refresh. First form submits action to php which determines which function (with yet another form) to show. But buttons interfere and I am nowhere near what I wanted to do.
Index.php is supposed to send user input to calculator.php which then opens next form depending on value:
<form id="form1" method="post" formaction="calculator.php">
<input id="form1" type="submit" value="Submit" />
<div id="parseSecondForm"></div>
<script type="text/javascript">
$(function(){
$('#form1').on('submit', function(e){
e.preventDefault();
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('formaction'),
data: $(this).serialize(),
beforeSend: function(){
$('#parseSecondForm').html('<img src="loading.gif" />');
},
success: function(data){
$('#parseSecondForm').html(data);
}
});
});
});
</script>
calculator.php receives user input and echoes next form in #parseSecondForm div in index.php:
if (form1 === '1') {
echo '
<form id="form2" method="post" formaction="form2.php">
<input id="form2" type="submit" value="Submit" />
<div id="thankyou"></div>
<!--submit saves php result in MySQL and thanks the user in next div-->
';
}
else {
echo '
<form id="form3" method="post" formaction="form3.php">
<input id="form3" type="submit" value="Submit" />
<div id="thankyou"></div>
<!--submit saves php result in MySQL and thanks the user in next div-->
';
}
I tried to echo modified javascript from index.php, but it just resets all the forms.
Maybe there are some form scripts designated for the cause or any other ideas how can I fix the issue?
In Javascript/HTML you cannot have two items with the same IDs:
<form id="form1" method="post" formaction="calculator.php">
<input id="form1" type="submit" value="Submit" />
form1 can be used only once.
Same for the php dynamically created forms.
I have a form, which take name from form and it sends to javascript codes and show in php by Ajax. these actions are done with clicking by submit button, I need to have another button, as review in my main page. how can I address to ajax that in process.php page have "if isset(submit)" or "if isset(review)"?
I need to do different sql action when each of buttons are clicked.
how can I add another button and be able to do different action on php part in process.php page?
<script type="text/javascript">
$(document).ready(function(){
$("#myform").validate({
debug: false,
submitHandler: function(form) {
$.post('process.php', $("#myform").serialize(), function(data) {
$('#results').html(data);
});
}
});
});
</script>
<body>
<form name="myform" id="myform" action="" method="POST">
<label for="name" id="name_label">Name</label>
<input type="text" name="name" id="name" size="30" value=""/>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<div id="results"><div>
</body>
process.php:
<?php
print "<br>Your name is <b>".$_POST['name']."</b> ";
?>
You just need to add a button and an onclick handler for it.
Html:
<input type="button" id="review" value="Review"/>
Js:
$("#review").click(function(){
var myData = $("#myform").serialize() + "&review=review";
$.post('process.php', myData , function(data) {
$('#results').html(data);
});
}
);
Since you have set a variable review here, you can use it to know that is call has come by clicking the review button.
Bind the event handlers to the buttons' click events instead of the form's submit event.
Use the different event handler functions to add different pieces of extra data to the data object you pass to the ajax method.
I have a form:
<form style="display: inline;" action="/player.php" method="post">
<input type="hidden" name="recname" value="'.$row['name'].'">
<input type="hidden" name="recordingdesc" value="'.$row['description'].'">
<input type="hidden" name="reclink" value="$_SESSION['customerid'].'-'.$row['timestamp'].'.wav">
<button type="submit" class="tooltip table-button ui-state-default ui-corner-all" title=" rec"><span class="ui-icon ui-icon-volume-on"></span></button>
</form>
and i want player.php to open in a modal dialog and be able to display the post information how can this be done.
Ajax is the answer. Post the form via ajax and in the callback function, (if the post was successful) you can create your dialog and load the data returned from the post. Check out Jquery's documentation on Jquery.post
First create a dialog using jquery-ui. You then need to ajax submit the form:
$("form button").click(function() {
$.post({url: '/player.php', data: $("form").serialize(),
success: function (data) {
$(div in dialog).html(data);
$("#MyDialog").dialog('open');
}
});
return false;
});