I want to pass html all tags value to another file using ajax.
is this possible
$.ajax({
url:"myp.php",
type:'POST',
data: //here I want to all possible element values in current html
});
Any Ideas???
$.ajax({
url:"myp.php",
type:'POST',
data: $(':input').serialize()
});
If you want to post every possible element values from a special form:
$('#myForm').serialize();
I think you could do the same with a different selector, which will include every elements to submit:
So, something like this should do the trick:
$('input, select, textarea').serialize();
You can see here for jQuery.serialize() : http://api.jquery.com/serialize/
And there for similar questions : Serialize form data to JSON
you can post all form's elements using this
data : $('#form').serializeArray();
Related
I have a PHP script that generates identical forms with different values (ie. lines of a database)
When one form is submitted, I want it to trigger an AJAX request that will update just that line of the database without reloading the page.
I have this AJAX script in my header:
function ajaxCall() {
$.ajax({
url:"database_quickupdate.php",
type: "POST",
success:function(result){
alert(result);
}
});
And obviously all forms have onsubmit="ajaxCall()" attributes set
But when I try to return the $_POST array from database_quickupdate.php, it comes back empty (meaning no data is passed to the script)
I tried various versions of serializing the data, including this here:
$.ajax({
type: "POST",
url: 'database_quickupdate.php',
data: $(this).serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
but this didn't work either.
Of course I can assign unique ID-s to each of the forms, but then how can I tell ajaxCall in the header that I want the values from the form that_has_just_been_submitted?
It must be something very basic, still, I'm lost. I think I'm missing something on the jQuery side, but I'm not even sure about that.
Thanks for your help
onsubmit="ajaxCall()"
You're calling ajaxCall() by itself, so inside it this is the default object (window in a browser).
So then:
$(this).serialize()
You are trying to serialize the window and not the form.
You need to pass the form.
Don't use on... attributes. They come with a host of issues.
Bind your event handlers with JavaScript instead.
jQuery("form").on("submit", ajaxCall);
That will pass the form as the value of this.
I need to fetch values of datatable cells having dropdowns that are selected. And pass the value to an array.
How can this be done using php, jquery?
Share with example
I am noob like you, but I've learned some php mysqli from here (note: always type misqli instead of mysql in your code)
https://m.youtube.com/watch?list=PL7lhMROXakvdLAUKFNLYl_J2Ti0Ulr6ID¶ms=OAFIAVgB&v=dWzzRkxrTMA&mode=NORMAL.
And about json:
alue into the the input's max attribute. Something like this will do the trick.
window.onload (//or what trigger method you need) = function() {
var (//name a var) = $('#idOfElementTogetValueFrom').val();
$.ajax(
{type: 'post',
url: 'path to php doc.php',
dataType: 'json'
data: {
NameX: nameOfVar declared above,
},
success: function (response){
$('#idOfelementWhereToDisplayphpResponse').html(response.matchthatnameinPhp);
}
});
}
In your php js variable will be like $_POST['NameX'] (do what you need with it)
Then in php
echo json_encode array(
"matchthatnameinPhp" => any php you need, vars
etc.(//this will be displayed in html)
(//You can add more rows of datas here)
)
See also: Send JSON data from Javascript to PHP?
I got the solution, by saving the data in an array and on submit button passing it to controller
I found many examples but I can't figure out how to apply it to my code. I'm adding a wish list button and want users to click it, get an alert that it was added to their member page and continue scrolling through content. I know I need to use ajax to send the variables from the url to javascript and then to php with ajax. But I'm stuck on getting those variables passed to ajax...probably because I'm still learning it. Using the GET in php is easy to get multiple variables but I can't figure out how to do it in javascript. And of course I don't want the page to reload. This is what I'm passing:
<div><a href='wish_list.php?title=".urlencode($title)."&link=".urlencode($link)."'
onClick='wish(this.href); return false;'>Add to Wish List</a></div>
How can I set these variables in javascript? Ajax I would use is:
$(function (){
set variables from url
$.ajax({
type: "POST",
url: "wish_list.php",
data: "$link and $title" //Need help with this here
success: function()...
});
});
I need the link variable to remain encoded because well it's a link. So how do I pull the title and link out of url and pass it to the php file?
js can't access php variables. You could store those values in a hidden html input field and then get it with jquery.
link = $(#IdHiddenField).val();
and then in the ajax function:
data: { link: link}
You'll need at least the basics of javascript and jquery to deal with the ajax functions.
You pass the parameters in the data attribute witch is an javascript object. It would look like this:
data: {"link":"value of link", "title":"value of title", "numbers_too":1},
You have to replace the values of those attributes above with your server side values.
You could put this data into the tag using data attributes:
<div<a class="sender" data-link="<?= $link; ?>" data-title="<?= $title; ?> href="wish_list.php" ... etc.
Then:
$(".data").click(function(e){
type: "POST",
url: $(e).attr("href"),
data: $(e).data()
});
You can add in the ajax function to use directy the "href" from link.
function wish(link){
$.ajax({
type: "POST",
url: link,
data: '',
success: function()...
});
}
With your link:
<div><a href='wish_list.php?title=".urlencode($title)."&link=".urlencode($link)."'
onClick='wish(this.href); return false;'>Add to Wish List</a></div>
Then, you get data from url with $_GET.
<div><a onClick=wish(urlencode($title),urlencode($link));>Add to Wish List</a></div>
function wish(title,link) {
$.post("wish_list.php", { title: title, link: link }).done(function() {
alert("Added to your member page");
});
}
I need a jQuery popup to ask the user for some required data before loading the same page it's on.
The data entered will become a php variable that I'll use to query a mysql table and pre-populate some form fields.
Any advice?
Thanks!!
you can make an AJAX call to the PHP and load it to div that you want. For the ajax calls you can use jquery it really makes you job easy.
eg:
$.ajax({
url: 'getitems.php',
success: function(data) {
$('#manage_inventory').html(data);
//alert('Load was performed.');
}
});
like in the example it is calling getitems.php and getting the list and loading it into #manage_inventory. The data being returned can be XMl or other type which can be parsed and be used according to your needs.
Your solution could be as simple as using a prompt() box in javascript and then passing the information via ajax
var stuff = prompt('Gimme Stuff');
$.ajax({
url: 'dostuff.php',
data: 'stuff=' + stuff,
success: function(data) {
//process stuff
}
});
I have some input like:
'<input type="text" name="display-count" class="display-count" value="23" autocomplete="off" readonly="readonly" />';
And I'm using this jQuery code to remove the value it has and replace it with another one:
var count = $('form.user-count-form').find('.display-count');
$(count).val('');
$(count).load("admin/user-count.php");
But the new value it isn't loaded. I have tried some other ways like:
$(count).val().load("admin/user-count.php");
or
$(count).val(load("admin/user-count.php"));
But didn't work as well. How do I do what I want ?
You need to use the JQuery get method here. You are improperly calling the script from your server and therefore, no call is actually firing and no data is being returned. I'm assuming that the value you are getting is a single string of either text or numbers.
the question is a bit vague so I'm not sure why you have the initial value set at 23 and I'm also not sure when you want to change the value so I am going to show you how to do it upon the dom being ready using the $(document).ready method.
$(document).ready(function(){
$.get('admin/user-count.php', function(data){
$('.display-count').val(data);
});
});
depends of what your script returns
you can do it so, for example:
$.ajax({
url: 'admin/user-count.php',
success: function( data ) {
$(count).val(data);
}
});
In additional you can return you user-count in JSON format, using php-function json_encode()
why dont your just try this
$('input.display-count').load("admin/user-count.php);
or try this onLoad of your HTML doc
$.get('admin/user-count.php', function(response){
$('input.display-count').val(response);
});
.load() will return the Html Contents to matched Element ,it wont set any value. Better you can use $.get or $.post or $.ajax
$.get('admin/user-count.php', function(resultdata){
$('input.display-count').val(resultdata);
});
or
$.ajax({
url: 'admin/user-count.php',
success: function( resultdata ) {
$(count).val(resultdata);
}
});