www.webdeveloper.com
Results 1 to 4 of 4

Thread: Jquery ajax help.

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Location
    UK
    Posts
    381

    Jquery ajax help.

    Can anyone please help me get my ajax post working? I cant get detailed error messages, all I know is that ajax php page doesnt return anything.

    Any help very much appreciated:

    Jquery:

    Code:
           $.post("ajax.php?action=add",
    		   	{ start: $.fullCalendar.formatDate(start, "yyyy-MM-dd HH:mm:ss"), end: $.fullCalendar.formatDate(end, "yyyy-MM-dd HH:mm:ss") },
                function(data){
    				 var fancyContent = ('<div class="header">Request sent</div>');
    			    $.fancybox({
                    content: fancyContent
                });
                }
    			, "json");
    Ajax.php

    PHP Code:


    <?php
    $dbhost 
    'xxx';
    $dbuser 'xxx';
    $dbpass 'xxx';
    $conn mysql_connect($dbhost$dbuser$dbpass);
    if(! 
    $conn )
    {
      die(
    'Could not connect: ' mysql_error());
    }


    $action $_GET['action'];
    if(
    $action =='add')
    start = ;start = $.fullCalendar.formatDate(start"YYYY-MM-dd HH:mm:ss");

     
    $sql "INSERT INTO events (". $.fullCalendar.formatDate(start'YYYY-MM-dd HH:mm:ss') .",". $.fullCalendar.formatDate(start'YYYY-MM-dd HH:mm:ss') . ")";


    mysql_select_db('importtest');
    $retval mysql_query$sql$conn );
    if(! 
    $retval )
    {
      die(
    'Could not enter data: ' mysql_error());
    }
    echo 
    "Entered data successfully\n";
    mysql_close($conn);

    }
    ?>
    Last edited by mattastic; 12-11-2012 at 10:43 AM.

  2. #2
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    500
    The data format of the response expected from the server is json yet your PHP script only echoes a plain text string.
    You could create an associative array named $output in your PHP script:
    $output=array();

    if(! $retval )
    {
    $output['msge]='Could not enter data: ' . mysql_error();
    }
    else{
    $output['msge]='Entered data successfully';
    }


    Then you can use:
    echo json_encode($output);

    Keep in mind the $output array can contains any sort of "key/value" data you want to send back to the front-end, thus the use of an associative array.

    Your javascript callback function can be written as:

    function(data){
    var fancyContent = ('<div class="header">'+data.msge+'</div>');
    $.fancybox({ content: fancyContent });
    }

  3. #3
    Join Date
    Jan 2005
    Location
    UK
    Posts
    381
    Thanks very much for your reply.

    I can't figure out how to get my ajax.phpto work.

    I keep getting the error:

    Could not enter data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

    Here is my query which I've used in the ajax.php code above.

    PHP Code:
    $sql "INSERT INTO events (start, end) values ("$_GET['start'] .","$_GET['end'] . ")"
    Could you tell me why its not working?

    Many thanks again

  4. #4
    Join Date
    Jan 2005
    Location
    UK
    Posts
    381
    FIXED thanks folks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles