I'm teaching myself jQuery and PHP and I'm having a few problems. I need the element id to be posted to my php script once dropped over my droppable div. The following code does not post when I drag and drop an element over a droppable div. I have tried the following:
I have removed everything apart from an alert box to check that an item has been dropped which it does, so the item is being seen as droppable!.
I have entered some content into the div manually to ensure results can been seen
I have replaced name to var name="test", to test the varible contents.
The javascript file and the php script reside in the same directory, file names are correct. The javascript drag and drop script is included after droppable area
<script type="text/javascript" src="js/drag-drop.js"></script>
I have replaced the result of php script as plain text to see if the problem could be the variable received, nothing returns. So the post does not seem to be getting to the php script. Hope someone can provide a few pointers!
Below is my code, the HTML is just a div.
$(document).ready(function() {
$('#carousel-ul li').draggable({
helper: 'clone',
revert: true
});
$('#drop').droppable({
hoverClass: 'drophover',
accept: '#carousel-ul li',
drop: function(){
//alert("has been accepted and dropped");
var name = ui.helper.attr('id').val();
$.post('javascript.php', { name: name }, function(data){
$('#drop-content').html(data);
});
}
});
});
<?php
//echo "id was posted";
if(isset($_POST['name'])){
$name = $_POST['name'];
var_dump($name);
echo name;
//echo "id was posted inside";
}
?>
The API documentation for the Droppable widget says that the ui parameter is a jQuery object, so you should try something like this:
$('#drop').droppable({
hoverClass: 'drophover',
accept: '#carousel-ul li',
drop: function(event, ui) {
var name = $(ui.helper).attr('id');
var content = $(ui.helper).html();
$.post('javascript.php', { name: name, content: content }, function(data) {
$('#drop-content').html(data);
});
}
});
Related
I've got a page with four divs on it that I'm trying to load using jquery. The user shows and hides the divs by clicking on links. Currently, the contents of the divs are populated with PHP include statements, but that leads to ~30-second load times for the page.
I'm trying to replace those include statements with JQuery in order to load the proper contents of each div when the user clicks the link to show or hide it. My problem is that when I click one of the links to switch divs, the div appears but without anything in it. I've used Fiddler to ensure the request is going off (which it is) but the response is coming back as a 404.
However, when I examine the response in Fiddler, the expected HTML is there, just not getting passed into my element properly. I've tried using .load() as well as $.get(), but got the same results. I've also ensured my element names are correct as well. Code is as follows:
$(document).ready(function()
{
$('#allvideos').click(function () {
$.get("/wp-content/themes/Danceamatic/templates/all_videos.php", function(data) {
var $resp = data;
$( "#carrusel_all_videos" ).html($resp);
alert("Load was performed.");
});
$('#current_showcase').hide();
$('#carrusel_new_videos').hide();
$('#carrusel_featured').hide();
$('#carrusel_all_videos').show();
$('#i_mentors').hide();
return false;
});
$('#featuredvideos').click(function () {
$.get("/wp-content/themes/Danceamatic/templates/view_featured.php", function(data) {
var $resp = data;
$( "#carrusel_featured" ).html($resp);
alert("Load was performed.");
});
$('#current_showcase').hide();
$('#carrusel_all_videos').hide();
$('#carrusel_new_videos').hide();
$('#carrusel_featured').show();
$('#i_mentors').hide();
return false;
});
$('#newvideos').click(function () {
$.get( "/wp-content/themes/Danceamatic/templates/new_videos.php", function( data ) {
$( "#carrusel_new_videos" ).html( $.parseHTML(data) );
alert( "Load was performed." );
});
$('#current_showcase').hide();
$('#carrusel_all_videos').hide();
$('#carrusel_featured').hide();
$('#carrusel_new_videos').show();
$('#i_mentors').hide();
return false;
});
$('#currentshowcase').click(function () {
$('#carrusel_all_videos').hide();
$('#carrusel_new_videos').hide();
$('#carrusel_featured').hide();
$('#current_showcase').show();
$('#i_mentors').show();
});
});
I've set breakpoints on the "var $resp = $data" line and have not hit it at all.
Any help is appreciated. Thanks!
The url "/wp-content/themes/Danceamatic/templates/all_videos.php" is not correct. Double check the location of the javascript file that makes this request and also check the location of the php file.
If the location seems correct then maybe it's the structure of the url -- try:
"wp-content/themes/Danceamatic/templates/all_videos.php" or
"./wp-content/themes/Danceamatic/templates/all_videos.php" or
"../wp-content/themes/Danceamatic/templates/all_videos.php".
everyone, i got a very simple problem, but i am really confused... for un hour
in a php page, i have the code:
<script>
$( document ).ready(function() {
$("#date_avant").click(function(){
$.post( "_corps_medecin_changedate.php", { name: 'test', id: '1' } );
$("#consultations_jour").load("_corps_medecin_changedate.php");
});
});
</script>
And in the page _corps_medecin_changedate.php, just at the beginning, i have the code:
<?php
var_dump($_POST);
$date_debut = $_POST['name'];
?>
And i got msg:
array (size=0)
empty
Notice: Undefined index: name
I checked the firebug, the parameters are correct in the post list, so i thinked it is sended correctly, but just canot receive in the action page.
Server info : Apache/2.2.11 (Win32) PHP/5.4.4
And in local envirment.
Can anyone help me?
Your returned data from your $.post call is not automatically made available to your .load() call. They are two separate functions which means your call to load essentially send no POST data to the PHP script.
You can pass the post parameters as part of your call to load
<script>
$( document ).ready(function() {
$("#date_avant").click(function(){
$("#consultations_jour").load("_corps_medecin_changedate.php", { name: 'test', id: '1' });
});
});
</script>
You have to handle result of post request in callback function, like this:
<script>
$( document ).ready(function() {
$("#date_avant").click(function(){
$.post( "_corps_medecin_changedate.php", { name: 'test', id: '1' },
function(result) { $("#consultations_jour").html(result);} );
});
});
</script>
In your code you make 2 different requests (first is in .post function, and second is in .load).
that is because your are calling the _corps_medecin_changedate.php again right (.load()), after the post is made with no posted datas.. and you are not using callbacks..
$("#consultations_jour").load("_corps_medecin_changedate.php"); //here
Since post is async... the .load() function (which isn't inside a post's callback) is called right after you call post and does not waits for it
i have no idea why are you loading the same php page again after post it made which i don't think is needed...but as an example ...you can use callback... to check the datas that you have posted
$.post( "_corps_medecin_changedate.php", { name: 'test', id: '1' },function(data){
alert('done');
});
//$("#consultations_jour").load("_corps_medecin_changedate.php");
try this and you should get the posted values in your PHP page.
In my script I'm experimenting with jquery draggable & droppable, See here. I'm trying to grab the data (to eventually put in a database) from each draggable element that's dropped into the droppable div. Right now I have it so that I can only get the data from the div the was last dropped.
$('#sortcard, #dropbox, #dropbox1').droppable({
accept: '.sorting',
hoverClass: 'border',
tolerance: 'touch',
drop: function(e, ui) {
$(this).append(ui.draggable.html() + '<br/>');
$("#add_friend").show().fadeOut(12000);
$(e.target).droppable("disable");
$(e.target).append("<input type='button' name='Sub' value='clear'/>").click(function() {
$(this).empty().droppable("enable");
});
var dropbox = $('#dropbox').html();
var dropbox1 = $('#dropbox1').html();
if(dropbox && dropbox1 !== '') {
$.post("account_main.php", {data: $(this).text()}, function(data) {
$('#demo').html(data);
});
}
}
});
I'm thinking the culprit may be
{data: $(this).text()}, function(data) {
$('#demo').html(data);
});
I have toyed with it and still not getting the results I desire. I feel maybe my coding on this is sketchy and needs a makeover, but I just need some ideas to push me in the right direction to make it efficient and would be very appreciative as well of any tips.
You're only getting the data from the last droppable because your sending $(this).text().
In that context $(this) is the last droppable that was dropped. Since you want more than that, you'll want to replace:
{data: $(this).text()}
with something like:
{
sortcard: $('div#sortcard').text(),
dropbox: $('div#dropbox').text(),
dropbox1: $('div#dropbox1').text()
}
Which will send the data from those three DIVs.
This piece of code (with another php file) grabs some content from a database and pus it into a div called about_content.
$(document).ready(function(){
$('a#about-menu').click(function() {
var id = $('a#about-menu').attr('class');
$.post('subpages/content_about/about_content.php',{id: id}, function(id){
$('div#about_content').text(id)
});
});
});
Everything works, but can you tell me how I can modify this so the stuff fades in, instead of just being smacked right in... I'm not sure how to use the fadeIn function in this scenario..
Try..
$('div#about_content').css('opacity', '0').text(id).fadeIn();
you can do
$('div#about_content').hide();
$('div#about_content').html(id);
$('div#about_content').fadeIn(1000);
didnt test it, so im not sure you could try
This fades out the existing content then replaces the content and fade it in!
$(document).ready(function(){
$('a#about-menu').click(function() {
var id = $('a#about-menu').attr('class');
$.post('subpages/content_about/about_content.php',{id: id}, function(id){
$('div#about_content').fadeOut('fast',function(){ $(this).html(id).fadeIn(); });
});
});
});
DEMO
I'm trying to load div content based on the a href being clicked and pass in a parameter. For example, Link 1
Clicking on Link 1 will pass value "3" to process.php, and return back the value "apple is good for you".
However, I can't seem to be able to pass the value without having a submit button. Anyway I can pass the parameter to another php file to process, and return the value?
$(document).ready(function(){
$("#testing a").live("click", function(evt){
var id= $(this).attr('id');
$.post("process.php", { id: id },
function(data) {
alert(data);
$('#result').load(data);
});
})
});
Below is my HTML
<div id="testing">
hello
</div>
<div id="result"></div>
Appreciate your help, thanks a lot!
You shouldn't be using numbers for id values. Instead, prefix them with a letter, or consider adding them to a data- atribute on the element.
Additionally, $.live() is deprecated, and we are encouraged to use $.on() from here on out for event delegation. I've gone ahead and handled this in the code below, but the id issue remains.
Lastly, $.load() and $.html() aren't the same. If you want to load data into an element, you don't call the load method (though the name could lead to that confusion).
// Short-hand version of $(document).ready();
$(function(){
// Handle anchor clicks on #testing
$("#testing").on("click", "a", function(e){
// Prevent links from sending us away
e.preventDefault();
// POST our anchor ID to process.php, and handle the response
$.post("process.php", { 'id': $(this).attr("id") }, function(data){
// Set the response as the HTML content of #result
$("#result").html(data);
});
});
});
From your process.php file, you might have something like the following:
$msg = array(
"...chirp chirp...",
"This is response number 1",
"And I am the second guy you'll see!",
"Apples are good for you!"
);
$num = $_POST["id"] || 0;
echo $msg[ $num ];