I'm trying to add AJAX pagination to my calendar, to avoid reloading the entire site as the calendar is located in a secondary tab, that reset to the primary tab on a reload.
Usually my "NEXT MONTH"-button would be wrapped in a link, that refreshed the same page with a GET-value of the date like this:
<a href="?date=<?php echo $nextDate; ?>">
<button type="button" class="btn">
NEXT MONTH
</button>
</a>
Which works just fine. But when I'm trying to pass the $nextDate value in a form to AJAX, and reload the page, it's as though it's not picking up the POST-value.
HTML:
<form method="post">
<input type="hidden" class="form-control" name="the_date" value="<?php echo $prevDate; ?>" />
<button type="button" data-name="next-month" class="btn">
NEXT MONTH
</button>
</form>
Javascript:
$("body").on( "click", "[data-name='next-month']", function() {
var the_date = $(this.form).find(".form-control[name=the_date]").val();
ajaxRequest = $.ajax({
type: "POST",
url: "index.php",
data: { the_date: the_date}
}).done(function (result) {
alert(the_date)
$("#calendar_container").load("index.php #calendar_container");
})
});
Am I going about this the right way, or is there some sort of reason why POST-values aren't picked up when sending them to the same url? If you have any suggestions as to how I can solve this, it would make my day!
When you are sending data write parameter name as a string constant.
data: { "the_date": the_date}
not
data: { the_date: the_date}
You making ajax request twice. The second without any parameters.
So replace next line
$("#calendar_container").load("index.php #calendar_container");
with
$("#calendar_container").html(result);
Related
So, right now I am trying to make it so that an AJAX post request can trigger this conditional statement:
if(isset($_POST['purse'])) {
if($_POST['purse'] == true) { <---- Removed
if($nexttime <= time()) {
/* Some DB queries etc.. */
$output = array();
array_push($output, "<div class='alert alert-success'>You are successful!</div>");
echo(json_encode($output));
} else {
/* Other stuff... */
}
}
/* Echos work here */
}
And so far I have tried this:
$(document).ready(function() {
$('#purse').click(function() {
$.ajax(
{
type : "POST",
url: "petty.php",
data: {purse: 'true'},
dataType: 'json',
success: function(data) {
alert(data);
}
});
});
});
But that doesn't seem to trigger it. This is all for an effort to make it so the page executes whatever it is supposed to, depending on which button the user clicks, without refreshing the page and I heard AJAX is the way to go for that, but I honestly can't figure it out and have tried tutorials but they don't do what I am trying to do, from what I have seen.
Form:
<form action="petty.php" method="post">
<div class="subheading mb-5 text-center"><br>Steal a few purses.
<button type="submit" name="purse" id="purse" class="btn btn-dark center-block">Submit</button>
<div class="subheading mb-5 text-center"><br>Forge checks.
<button type="submit" name="checks" id="checks" class="btn btn-dark center-block">Submit</button>
<div class="subheading mb-5 text-center"><br>Steal cars.
<button type="submit" name="cars" id="cars" class="btn btn-dark center-block">Submit</button>
</form>
EDIT: Well, my issue is that I am trying to push all messages, that the user will be able to see upon clicking a button, into an array and using echo(json_encode($output)) to push it all to AJAX so that it can be outputted to the browser, but it doesn't seem to be working.
You're using
$.ajax({
url: url,
data: {'purse' : purse},
type : "POST"
});
but we don't see the definition of 'url' or 'purse' anywhere. Did you debug it using Chrome/Firefox Developer Tools and check if it actually sends the ajax request?
Also, you should first check the existence of the ['purse'] key in your php code and then check validity. You're only checking existence, so the if statement will be entered any time you send the ajax, regardless of whether 'purse' actually contains anything.
I am trying to post data through my website via ajax, however for some reason it seems to echo the success message but does not get the mobile field data. it's just blank. When i inspect with firebug, it gives the response, but the POST tab is empty.
My controller contains the following :
function submit()
{
//set validation rule
// get post data
$emailid = $this->input->post('mobile');
// write your database insert code here
echo "<div class='alert'>Thanks for Subscribing! Please stay tuned to get awesome tips...</div> here is $emailid";
}
And my view contains :
<label>Mobile</label>
<input type="number" name="mobile" id="mobile" class=" form-control" />
<button type ="submit" id="submit" name="submit" class="btn btn-info btn-block" /> NEXT </button>
The JS
<script type="text/javascript">
$("#submit").click(function(e) {
e.preventDefault();
var mobile = $("mobile").val();
$.ajax({
url: "https://cheddarplatform.com/complete/submit",
method: "POST",
data: {mobile: mobile},
success: function(data) {
$("#message").html(data);
},
error: function() {
alert("Please enter valid email id!");
}
});
});
</script>
$("mobile") is not a proper selector, as you do not have a HTML element of that type anywhere. Probably you want to use $("#mobile") and have a look at your browser's network tab to find this error easier the next time
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(),`
The problem that we have is describing as follows. We have two different pages, the first one is called profile.php and the second one is called save_button.php. In the page profile.php I have this script which is written the following code in jQuery:
<script >
$(function(){
$("#save").bind('click', function(){
document.getElementById('save').style.background='#F26522';
document.getElementById('modify').style.background='#0083A9';
$.ajax({
url: 'save_button.php',
type: 'post',
data: { "callFunc1": "test"},
success: function(response) { alert(response); }
});
})
});
</script>
In the body of profile.php page we have written this code, here we have two different buttons:
<body>
<div class="button_container" >
<input id="modify" type="button" class="buttons" value="Modify" onclick="changeText();" />
<button id="save" class="buttons" >Save</button>
</div>
</body>
When we click the save button we want to execute the PHP code in another page, called save_button.php. Inside this page, save_button.php, we have written the following code:
<?php echo "test works"; ?>
What happens with us is that when we click the save button, in our page is not alerted the words "test works". Do you have any idea what to do in this page? Please, give us a quick feedback as this is a school project and we have to submit within the three next days. Many thanks.
I'm clearly doing something wrong here, but I can't figure out why the Ajax isn't firing and instead insists upon a page load. The newBatable() fires fine, I just can't seem to get the vote to respect the ajax call.
HTML - not sure how to put html in here as code :/ - I feel dumb.
<form class="form-horizontal" id="batable1" action="vote.php" method="GET">
<div id="success-vote-1"></div>
<input type="radio" name="batableResult" value=" include ()" /> include ()<br/>
<input type="radio" name="batableResult" value="require ()" />require ()<br/>
<input type="radio" name="batableResult" value="both of above" />both of above<br/>
<input type="radio" name="batableResult" value="None of above" />None of above<br/>
<button class="btn btn-primary" onClick="vote(1)">Vote</button>
<input type="hidden" name="batableId" id="batable-id" value="1"/>
</form>
JS - the console display everything I want, the php script processes everything nicely and functions perfectly, it is just it has to load the php in the browser so it's not using AJAX
/***************************************/
function newBatable() {
var batableData = $('#new-batable').serialize();
//console.log(batableData);
$.ajax({
url: "process.php",
data: batableData,
success: function(data){
$('#success-new-batable').html(data);
}
});
}
/***************************************/
function vote(poll_id) {
//console.log(poll_id)
var batableId = "#batable" + poll_id;
//console.log(batableId)
var pollData = $(batableId).serialize();
//console.log(pollData);
$.ajax({
url: "vote.php",
data: pollData,
success: function(data){
var batable_success_id = "#success-vote" + poll_id;
$(batable_success_id).html(data);
}
});
}
The submit button fires the JavaScript and then immediately submits the form.
If you are using onclick, then return false to stop that.
You would be better off using a more modern event binding technique though.
how about attaching a click event via jquery to the button?
$(".btn").on('click', function(e){
e.stopPropagation()
e.preventDefault();
vote(1);
});
this would usually be placed in document .ready jquery in an external file or somewhere near the bottom of your page inside script tags.
Does this help?
Since you're already using jQuery, as SubstanceD, you should use jQuery's on() method and stop the event propagation and prevent the default action (submitting the form).
I also noticed a possible bug in your code. It looks like there is a typo. You have
var batable_success_id = "#success-vote" + poll_id;
and <div id="success-vote-1"></div>. You have a dash after vote in the div's ID while you are concatenating batable_success_id into #success-vote1, for example. So even if the AJAX call is made, it probably won't update your HTML like you're expecting.