-1

I have finally managed to get a form to post data using ajax to a php file however i keep encountering the following error:

"Notice: Undefined index: course_title in /Applications/XAMPP/xamppfiles/htdocs/insights/ManageCourses_UpdateSubmit.php on line 26

Notice: Undefined index: course_code in /Applications/XAMPP/xamppfiles/htdocs/insights/ManageCourses_UpdateSubmit.php on line 27 NULL NULL record updated"

i find to be bizarre because it does update the record fine when i check the mysql table and when i refresh the page the updated values are shown.

the ajax script which is triggered when the button is clicked is:

    <script>
function myCall() {
    var request = $.ajax({
        url: "ManageCourses_UpdateSubmit.php",
        type: "GET",            
        dataType: "html"
    });

    var data = $('#updateForm').serialize();
    $.post('ManageCourses_UpdateSubmit.php', data);

    request.done(function(msg) {
        $("#updateForm").html(msg);         
    });

    request.fail(function(jqXHR, textStatus) {
        alert( "Request failed: " + textStatus );
    });
}
</script>

I have noticed when i remove this from the AJAX code i don't get the error message however i need the page to refresh once the value has been updated.

  request.done(function(msg) {
        $("#updateForm").html(msg);         
    });

    request.fail(function(jqXHR, textStatus) {
        alert( "Request failed: " + textStatus );
    });

sorry about that forgot to submit the main file

the ManageCourses_UpdateSubmit.php file is:

    <?php

 include "db_conx.php";

try
{
    $db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname",    $mysql_username, $mysql_password);

    $db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = $db_conx->prepare("UPDATE course_details SET course_title = :course_title 
    WHERE course_code = :course_code");

$course_title = $_POST['course_title'];
$course_code = $_POST['course_code'];
echo var_dump($course_title)."<br>";
echo var_dump($course_code)."<br>";

$sql->bindParam(':course_title', $course_title, PDO::PARAM_STR);
$sql->bindParam(':course_code', $course_code, PDO::PARAM_STR);


/*** execute the prepared statement ***/
$sql->execute();

/*** success message ***/
$message ='record updated';
} 
catch(Exception $e)
{
    $message = 'Message: ' .$e->getMessage();
}


 ?>
<html>
<head>
<title>Update Course</title>
</head>
<body>
 <p><?php echo $message; ?>
 </body>
 </html>

any ideas?

user90210
  • 115
  • 4
  • 17

1 Answers1

1

You are making 2 requests. The 1st is a GET request, so there are no POST variables set. There is no need for this request, the post request will also return a response, so you can use that:

function myCall() {

    var data = $('#updateForm').serialize();
    $.post('ManageCourses_UpdateSubmit.php', data, function(response){
        //display message
        $("#updateForm").html(response);
        //'soft'reload parent page, after a delay to show message
        setTimeout(function(){
            window.location = window.location;
        },1000);


    }).fail(function(jqXHR, textStatus) {
        alert( "Request failed: " + textStatus );
    });
}

Also, note that you dont want the response to contain <head> and <body> tags, as it is being added to an existing page, so ManageCourses_UpdateSubmit.php should end like this:

catch(Exception $e)
{
    $message = 'Message: ' .$e->getMessage();
}

die($message);
//nothing else after this
Steve
  • 20,703
  • 5
  • 41
  • 67
  • you are wonderful. it works great. is there a way to refresh the page after the call to the php? – user90210 Mar 02 '15 at 14:08
  • Yes, but why do you need to refresh the page? – Steve Mar 02 '15 at 14:09
  • basically there is a modal (pop up window) which appears when a user wishes to edit row information in a table. so i was trying to use ajax to trigger off the call to the php file and return the result and then update the main html page where the table is so the user can see the table row information updated. – user90210 Mar 02 '15 at 14:12
  • thanks for the fast update. Just tried this and it still shows the modal page when i click the update button. Is it close the pop up using ajax? – user90210 Mar 02 '15 at 14:25
  • Yes, you will need to close the modal. I dont know what library you are using for the model, so i cant help with the specific code, but you would put the close code within the timeout, just before the `window.location` line – Steve Mar 02 '15 at 14:30
  • oh i see! thanks, ill play around with it. I'm using bootstraps javascript for the modal. thank you very much for your help steve! – user90210 Mar 02 '15 at 14:40