I'm pulling my hair out over this one - I have a page that searches a MySQL database and returns the results in a table. I want the user to be able to update the results and hence update the MySQL database. The first part works ok, I have a search box on the page, which uses jQuery/ajax to query the database and display the results, e.g.:
<form class="well" id="jquery-submit-ajax" method="post">
<input type="text" name="userSearch" placeholder="Search…">
<label>Name/Email/id</label>
<Br />
<input type="submit" id="searchButton" value="Search">
</form>
<div class="alert alert-success hide">
<div id="success-output" class="prettyprint"></div>
</div>
<div id="loading-image" class="alert hide" style="text-align:center">
<img src="../images/ajax-loader.gif" /></div>
<div class="alert alert-error hide">
<div id="error-output" class="prettyprint"></div>
</div>
</div>
and the jQuery:
$("#jquery-submit-ajax").submit(function(e) {
$('#loading-image').fadeIn();
$.ajax({
type: "POST",
url: "jquery-ajax-control.php",
data: $(e.target).serialize(),
dataType: "html",
beforeSend:function(){
$('.alert-error,.alert-success').hide();
},
error: function(jqXHR, textStatus, errorThrown){
$('#loading-image').hide();
$('.alert-error').fadeIn();
$('#error-output').html(errorThrown);
},
success: function(data){
$('#loading-image').hide();
$('.alert-success').fadeIn();
$('#success-output').html(data);
}
});
return false;
});
So the results are parsed into a table for each result. Within that table is a form with a submit button. e.g.:
<form method="post">
<table>
<tr>
<td><input type="text" class="editbox" name="subs-expiry" value="<?php echo $expiry_date; ?>"</td>
</tr>
</table>
<input type="submit" class="updateSubsButtons" value="Update" />
</form>
I'm trying to submit this form via jQuery/Ajax again, but I can't get it to work. Pressing the Update button results in the whole page refreshing. I've stripped the jQuery for this button right back to just display an alert when the button is pressed, but even that doesn't work.
$(".updateSubsButtons").on("submit", function(e){
alert("clicked");
e.preventDefault();
});
Using the Chrome debugger a breakpoint in the function never gets hit, so maybe jQuery can't find the button? I've tried .on() and .live() functions, but I get the same result with both. I really can't figure out what's going on here, so any help would be gratefully received!
Try replacing it with a button, and a 'click' event.
<button class="updateSubsButtons">Update</button>
$(".updateSubsButtons").on("click", function(e){
alert("clicked");
console.log(e);
e.preventDefault();
});
Here's how to deal with asynchronously loaded items(added because this is how the problem was actually fixed.):
$(".AlreadyLoadedParent").on("click",'.buttonYouWantToClick', function(e){
alert("clicked");
console.log(e);
e.preventDefault();
});
First, you "click" a button, you don't "submit" it. You submit the form. So you should work with the event "click".
Second, if the button that should trigger the jQuery code is loaded using AJAX you should bind the event using .live() function like this:
$('.updateSubsButtons').live('click', function(e) {
e.preventDefault();
// Your code here...
}
This way the click event is bound to every new object with class="updateSubsButtons" that is loaded even after the page is loaded. If you use $('.identifier').click() it only works on objects that are already loaded when the page loads and doesn't work on elements being loaded through AJAX.
Related
I am trying to send form data using ajax. My index page the records are showing after having been fetched using ajax from remote page.
Below is my page image.
Each record has comment box I want to store these comments in a data base using ajax.
Below is my jquery
$(function(){
$('body').on('submit','.comment_p',function(){
var post_id = $("#post_id").val();
var com_dis= $("#comment_disc").val();
var cominfo = 'id=' + post_id + '&disc=' + com_dis;
if(com_dis=='')
{
alert('Please add your comment');
} else{
$.ajax({
type:"POST",
url:"/comment_update.php",
data:$(".comment_p").serialize(),
success: function(data){
alert(data);
}
});
}
return false;
});
});
i used body on click because these records are loaded from remote page.
and my form is below
<div class="panel-zesteve-textarea">
<form method="post" id="'.$row['id'].'" class="comment_p">
<input id="post_id" type="hidden" name="post_id" value="'.$row['id'].'">
<textarea id="comment_disc" name="comment_disc" rows="2" cols="48"></textarea>
<button id="com_submit" type="submit" class="btn btnbg">Post comment</button>
<button type="reset" class="[ btn btn-default ]">Cancel</button>
</form>
nowWhen I click on post, comment is working for one record that is the last record. I need to send id no. and textarea value to php page to update in mysql, but both comments are showing same record id and comment. It's not working for sencond one
Try to reference the form with $(this)
data: $(this).serialize(), instead of `data:$(".comment_p").serialize(),`
I have a form...I want to add disabled to all the inputs inside a certain div but rest of the form. I verified that this is indeed happening as it should but, when I submit the form with the disables added, I get no POST data. But with all my code the same, except for not adding the disableds, I do get my post data as I should. And I repeat, I DID verify that the disableds are only going on the inputs that I expect and NOT the other inputs that I want data from.
There is not really even much code..the inputs look like this (and it is definitely inside a form element) -
<div class="abridged-hide">
<input type="hidden" name="actor_id[]" value="162705079">
</div>
The submit button -
<button type="submit" id="entry_submit" class="btn btn-primary">Update Page</button>
Jquery is simply -
$('#entry_submit').on('click', function(event){
$('.abridged-hide input').attr('disabled', 'disabled');
$('.unabridged-hide input').attr('disabled', 'disabled');
});
I tried
.attr('disabled', true);
.prop('disabled', true);
Like I said, if I comment out the Jquery and submit, I do get my post data fine, something about this Jquery is making me not get my post and really don't see any reason why... Thanks in advance.
edit - these are the inputs being created dynamically.
<td class="btn btn-primary">
<span class="glyphicon glyphicon-resize-vertical" aria-hidden="true">
<input type="hidden" name="actor_id[]" value="162705079">
</span> Julie Benz - J</td>
edit - code that generates the inputs
$( ".drop-container" ).droppable({
accept: ".draggable",
addClasses: false,
drop: function(event, ui) {
var drag_name = ui.draggable.html();
ui.draggable.hide();
var new_body_count = '<tr><td class="btn btn-primary">'+drag_name+'</td>
<td class="badge"><input placeholder="Number of Kills" name="body_count" class="number-count" type="number"></td></tr>'
$('.table').prepend(new_body_count);
}
});
First, let's get this out of the way:
No errors (JS, etc) exist on the local and remote dev page. The jQuery lib is also called correctly.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
Basically both pages are identical.
What I'm trying to have my webpage achieve:
Submit my form if the user (accidentally) clicks away on a link instead of using the normal form submit button.
Link:
<a class="arrow autoSaveLeft" href="<?php echo $prevWeekURL ?>">←</a>
<form>
// ...
<input type="submit" name="submit" class="submitForm" value="Update" />
</form>
JS (at bottom):
<script>
$(document).ready(function() {
$('.autoSaveLeft').click(function(event){
event.preventDefault();
$('.submitForm').click();
window.location = $(this).attr('href');
});
});
</script>
Any idea why this might fire correctly on MAMP (form is submitted and url is followed), but not when I try live on a shared host (form is NOT submitted, but url is followed)?
Thank you.
If you wanted to save your user's progress on a form before leaving, you'd have to submit the form without actually, submitting it via the browser. Thus you could use AJAX to send the information that's been provided so far, upon success - carry on with the href provided on the original .click() handler.
<a class="arrow autoSaveLeft" href="somepage.php">←</a>
<form id="userform">
<input type="submit" name="submit" class="submitForm" value="Update" />
</form>
<script>
$(document).ready(function() {
$(document).on('click', '.autoSaveLeft', function( event ) {
var $that = $(this);
event.preventDefault();
$.ajax({
type: "POST",
data: $('#userform').serialize(),
url: 'yourpostscript.php',
success: function(){
window.location.href = $that.attr('href');
}
});
});
});
</script>
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.
Need some help with a login form please.
I've set up a login form through ajax, but it's not working when the username/password are incorrect.
The php script works perfectly when not used through ajax. It does what it's supposed to.
When submitting it through an ajax call, if the details are incorrect a new form pops up and asks for you to try again. That's good, but I can't resubmit the form. Clicking on the Submit link doesn't work.
Here's the code:
As an aside, the ajax doesn't use "submit()" to load the ajax. There is a separate link outside the form that needs to be on a "click()".
<script>
$(document).ready(function(){
$("#login_submit").click(function() {
$("#form2").hide();
$('#finish').html('Attempting Login');
var username = $("input#username").val();
var password = $("input#password").val();
$.ajax({
url: 'auth.php',
type: "POST",
data: "username="+username+"&password="+password+"",
success: function(data) {
$('#finish').html(data);
}
});
});
});
$(document).ajaxStart(function(){
$('#loading').show();
}).ajaxStop(function(){
$('#loading').hide();
});
</script>
<div id="form2">
<form method="post" id="login">
<input type="text" name="username" id="username"/><br />
<input type="text" name="password" id="password"/><br />
</form>
<span id="login_submit">Submit</span>
</div>
<div id="loading">
Loading...<img src="loading/loading6.gif" width="105" height="16" alt="loading" />
</div>
<div id="finish">
</div>
So as I said, it loads the form to resubmit, but clicking on "Submit" doesn't resend the information.
Any tips for getting this to resubmit?
data: "username="+username+"&password="+password+"",
Try as:
data: "username="+username+"&password="+password,
Answered my own question, sorry guys.
I changed the ajax to check for certain data being returned in the success callback, and changed the form based on that data, instead of replacing the form completely.
I'll post the changes in a bit