HTML
<a id="1">Add to notebook</a>
<a id="2">Add to notebook</a>
<a id="3">Add to notebook</a>
<a id="4">Add to notebook</a>
...
<a id="40">Add to notebook</a>
JavaScript
<script>
$(document).ready(function(){
//callback handler for post submit
$("a").click(function(e)
{
var vocab_id = $(this).attr("id");
var formURL = 'http://localhost/test/test.php';
$.ajax(
{
url : formURL,
type: "POST",
data : vocab_id,
dataType : "text",
});
e.preventDefault(); //STOP default action
//e.unbind(); //unbind. to stop multiple form submit.
});
});
</script>
I have a list of hyperlinks, it will send the currently clicked value of id to the server end by clicking the respective link.
But the problem is
Back-end doesn't receive any post data at all.
People said that I should uncomment the e.unbind() to stop multiple submit, but I got error after that line was uncommented. Error message is like this.
Uncaught TypeError: undefined is not a function 2:479(anonymous function) 2:479n.event.dispatch jquery-1.11.0.js:3r.handle
The data part in jQuery.ajax() should be like either
data :{ name : vocab_id }
or
data : "name="+vocab_id
Syntax for unbind() is not correct use the following
$(this).unbind(e);
it will unbind the object from current event handler
Related
Thanks for reading. I have tried the answers in other similar questions but none have worked so far. I'm trying to UPDATE values inside a Table that is inside a form, so in the first part of the file is the isset "saveImport" which is the name of the a tag that I'm using to send the id thru the URL:
if (isset($_POST['saveImport'])) {
$id = $_POST['id'];
}
a tag:
<a name="saveImport" href="./?id=<?= $row['id']; ?>" class="saveImport btn btn-success col-xs">Save</a>
I do get the ID value in the URL but since is been updated in the same file I'm assuming it refreshes the page before the variable gets post thru AJAX:
<script type="text/javascript">
$(document).ready(function() {
$('.saveImport').click(function() {
var imp_id = id.id;
var imp_href = $(id).attr('href');
alert(imp_href);
$.ajax({
url: "./",
data: {id : imp_id},
type: "POST",
success: function(data){
alert(id);//send this ID to the PHP variable without
//refreshing the file
}
});
});
});
I'm getting the class .saveImport because it's inside a Table which is displaying values from a Database so it's inside a while loop and if I use an ID it will cause conflict as the ID will repeat itself.
I already created the PHP function to UPDATE the Values which executes if the ISSET is true, so my real problem will be to execute the ISSET and inside grab the ID that was posted with AJAX so I can use the ID in the UPDATE function. Right now, if I click on the a tag, it sends the ID thru URL, the page refreshes but the value of the ID is not getting in the $id = $_POST['id];
I appreciate your time.
This should work.
Change the ajax url to your php file name (mine was t.php).
Replace 99 with your data from php. (id is used. href is not used)
<?php
if (isset($_POST['saveImport'])) {
print_r( $_POST );
exit;
}
?>
<a name="saveImport" id='99' href="./?id=99" class="saveImport btn btn-success col-xs"'>Save</a>
<script type="text/javascript">
$('.saveImport').click(function(event) {
var imp_id = this.id;
$.ajax({
url: "t.php",
data: {id : imp_id, saveImport: 1},
type: "POST",
success: function(data){
alert( data );//send this ID to the PHP variable without
//refreshing the file
}
});
event.preventDefault();
});
</script>
.preventDefault() will prevent the link from loading the target page... And then, ajax will proceed.
$('.saveImport').click(function(event) {
event.preventDefault();
I have this code below that gets the data from the POST and return the data as a table. When the code runs I put Console Log to see what is happening once the SUBMIT button is pressed. When the console log shows as "done" I want to show the table instead, but can't see why it's not happening.
<script type="text/javascript">
$(function(){
$('#searchform').on('submit', function(e){
e.preventDefault();
//alert($('#searchpostcode').val())
$.post('includes/jobdetailssearch.php',
$('#searchform').serialize(),
function(data, status){
$('.table-responsive #displayadd').html(data.Display);
//$("#table-responsive td").last().append(data);
console.log("done");
}).fail(function () {
console.log("fail");
});
});
});
</script>
Why would this be?
Make sure your php script returns a serialized json structure, which can be parsed by javascript.
Try to console.log(data) in your callback function to see what is returned from PHP.
Try This:
<script type="text/javascript">
$(function(){
// Submit Button Pressed
$(document).on('submit','#searchform', function(e){
// Prevent Default Click
e.preventDefault();
//The first parameter of $.post() is the URL we wish to request
$.post("includes/jobdetailssearch.php",
{
//Then we pass in some data to send along with the request
searchval:$('#searchform').val()
},
//The third parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
Note:
$('#searchform').on('submit' work same as $('#searchform').submit( but when you use $(document).on('submit','#searchform' then it will also work for the DOM added later.
Using AJAX to pass form values to a PHP page. Once the values are passed, i'm clearing the values of INPUT fields in the form. It works fine, but when i clear the input:date value the page gets Refreshed !
$('#form1').submit(function(){
var formdata = $(this).serialize();
$.ajax({
type:'POST',
url:'certification.php',
data:formdata
});
$(this).find('input:text').val('');
$(this).find('input:file').val('');
$(this).find('input:date').val(''); **On adding this line page gets Refreshed.**
return false;
});
Jquery does not support that selector.
so you getting.
Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: date
your code should look like:
$('#form1').submit(function(e){
e.preventDefault(); //this will prevent default action even if there is uncaught exeption.
var formdata = $(this).serialize();
$.ajax({
type:'POST',
url:'certification.php',
data:formdata
});
$(this).find('input:text').val('');
$(this).find('input:file').val('');
$(this).find('input[type="date"]').val('');
});
http://jsfiddle.net/bz3s6/
I'm using the jEditable plugin and the following code to toggle between On and Off for a series of settings on a page.
$(document).ready(function() {
$('.editable_select').editable(''http://someexamplepage.com/save.php', {
indicator: '<img src="/images/spinner.gif">',
data : " {'✓':'✓','✗':'✗'} ",
tooltip : 'Click to Edit',
type : 'select',
onblur : 'submit',
style : 'inherit'
});
});
And then this in the html:
<b class="editable_select" id="setting1" style="display:inline">✓</b>
When the checkmark indicating On is clicked, it produces a dropdown menu with checkmark for On and the X for Off in it, which the user can then select. What I would prefer is that clicking the check/X not open a dropdown, but instead send the current On or Off setting to the save.php file. I could then just write the save.php file to return the opposite value, so that clicking just toggles between the two without opening any kind of edit window. I tried the following code:
$(document).ready(function() {
$('.editable_select').editable('http://someexamplepage.com/save.php', {
indicator: '<img src="/images/spinner.gif">',
tooltip : 'Click to Edit',
onclick : 'submit',
style : 'inherit'
});
});
But clicking the text still opens a little editing window, which I don't want. I'm still new to JavaScript and jQuery, so any help is greatly appreciated!
I wouldn't use a plugin for this, but rather a very simple bit of jQuery to run on the ready event.
I would include the current state as a data attribute on the DOM, so change your tags to:
<b class="editable_select" id="[ID FOR ITEM]" data-state="checked">ઙ</b>
Then do something like:
$(function(){
var update_dom = function(data){
var item_id = data.id;
var state;
data.state === 'checked' ? state = '✓' : state = '✗';
$('#'+item_id).data('state', data.state).html(state);
};
var selected = function(evt){
var item_id = $(this).attr('id');
var state = $(this).data('state');
$.ajax({
url: PATH_TO_SCRIPT,
data: {id: item_id, state: state},
type: "POST",
success: update_dom
});
}
$('.editable_select').click(selected);
});
This binds the click event to everything that has a class of editable_select. When you click on it, it'll call the selected function, which wil get the data from the DOM, and call your script. When your script is complete, it should send back a response (JSON would be good here), and, the success handler will update the DOM and display the state back to the user.
I need to run a PHP code from external server when user clicks a link. Link can't lead directly to PHP file so I guess I need to use AJAX/jQuery to run the PHP? But how can I do it and how can I pass a variable to the link?
Something like this?
<a href="runcode.html?id=' + ID + '"> and then runcode.html will have an AJAX/jQuery code that will send that variable to PHP?
use something like this in you page with link
Some text
in the same page put this somewhere on top
<script language='javascript'>
$(function(){
$('.myClass').click(function(){
var data1 = 'someString';
var data2 = 5;//some integer
var data3 = "<?php echo $somephpVariable?>";
$.ajax({
url : "phpfile.php (where you want to pass datas or run some php code)",
data: "d1="+data1+"&d2="+data2+"&d3="+data3,
type : "post",//can be get or post
success: function(){
alert('success');//do something
}
});
return false;
});
});
</script>
on the url mentioned in url: in ajax submission
you can fetch those datas passed
for examlple
<?php
$data1 =$_POST['d1'];
$data2 =$_POST['d2'];
$data3 =$_POST['d3'];
//now you can perform actions as you wish
?>
hope that helps
You can do this with an ajax request too. The basic idea is:
Send ajax request to runcode.html
Configure another AJAX to trigger from that page
Considering, this as the markup
<a id="link" href="runcode.html'">Test</a>
JS
$("#link").on("click", function() {
$.get("runcode.html", { "id" : ID }, function(data) {
//on success
});
return false; //stop the navigation
});