user90210
user90210

Reputation: 115

undefined index - form not sending value through to php code via javascript

Receiving the following error message when trying to add a new record in the database:

Notice: Undefined index: course_title in /Applications/XAMPP/xamppfiles/htdocs/insights/ManageCourses_AddSubmit.php on line 13 NULL Message: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'course_title' cannot be null

I have similar queries which update and delete records in the table and they work fine so I'm not sure why there's a problem with this.

it says NULL for the course_title however in the javascript i serialise the form information so I'm not sure why its not sending it.

the course_details table has an auto-increment field which is automatically updated when a record is added.

Could anyone shine some light on what could be the issue please?

javascript which handles what happens when submit is clicked:

function addCall() {
  var data = $('#addForm').serialize();
  $.post('ManageCourses_AddSubmit.php', data, function(response){

    $("#addForm").html(response);
  }).fail(function(jqXHR, textStatus) {
    alert( "Request failed: " + textStatus );
  });
}

the modal where the form is which gets submitted:

<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title">Add New Record: </h4>
                                    </div>
                                    <div class="modal-body">
                                        <form id="addForm" class="addForm">
                                            <div class="form-group">
                                                <label for="course_code" class="pull-left" class="control-label">Course Code:</label>
                                                <input type="text" class="form-control" id="ourse_code_id" name="code[]" readonly value ="NULL">
                                            </div>
                                            <div class="form-group">
                                                <label for="course_name" class="pull-left" class="control-label">Course Title:</label>
                                                <input type="text" class="form-control" placeholder="Enter Course Title" id="course_name_id" name="title[]">
                                            </div>
                                        </form>
                                    </div>
                                    <div class="modal-footer">
                                        <div class="btn-toolbar">
                                            <button type="button" class="btn btn-primary" id="add_row" name="add_row">Add New Record <span class="glyphicon glyphicon-plus"></button>
                                            <button type="button" class="btn btn-default" class="pull-right" data-dismiss="modal">Close</button>
                                            <button type="button" class="btn btn-success" class="pull-right" onclick="addCall();">Submit <span class="glyphicon glyphicon-saved"></button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

finally the php file:

<?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("INSERT INTO `insights`.`course_details` (`course_code`, `course_title`) VALUES (:course_code, :course_title)");

        $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 = "<p class='text-success'> Record Successfully Added <span class='glyphicon glyphicon-ok'/></p>";
    } 
    catch(Exception $e)
    {
        $message = 'Message: ' .$e->getMessage();
    }


die($message);
?>

I've noticed when i use back ticks around (:course_title)it adds the record in fine however the value added is :course_title

Many Thanks

Upvotes: 0

Views: 88

Answers (1)

peter_the_oak
peter_the_oak

Reputation: 3710

I think it's a naming mistake.

In your form, you have this input field:

<label for="course_name" class="pull-left" class="control-label">Course Title:</label>
<input type="text" class="form-control" placeholder="Enter Course Title" id="course_name_id" name="title[]">

There you call it course_name_id. Then in your PHP script, you try to get these variables:

$course_title = $_POST['course_title'];
$course_code = $_POST['course_code'];

There you call it course_title.

I suggest you try to clean up all names used. If the table field is called course_title, then use this name for the form and for the PHP controller.

Or, a more elegant approach, try to outsource all names into some XML config files, so your controller does always the same work based on config files :-)

Upvotes: 1

Related Questions