Blinkydamo
Blinkydamo

Reputation: 1584

Combine these scripts to work together, Javascript/AJAX and PHP

I am learning all the languages involved here and currently struggling to combine two scripts to do a single task.

I want to be able to select a branch from a form option list, send that value from the option to the php script to query the database. The information returned from the database is then to be used on a page that is loaded using the second script.

Jquery/AJAX script one - Used currently to load a php page into a DIV using the value collected from the users selection.

<script>
    $(document).ready(function(){
        function loadBranch(branch) {
            if (branch) {
        $('div#content2').load('../../procedures/'+ branch +'/ClientCalls/' + branch + '-Bookings.php');
        }
    }
    $('select[name=branch]').on('change', function() {
        loadBranch($(this).val());            
        });
    loadBranch($('select[name=branch]').val());
    });
</script>

The second script I am trying to use to populated the page the previous script has loaded.

<script>
$(document).ready(function(){
    $('select[name=branch]').on('change', function() {var branchGet = this.options[this.selectedIndex].value
        console.log(branchGet); 
        $.ajax({                                     
            url: '../../procedures/'+ branchGet +'/ClientCalls/' + branchGet + '-Bookings.php',
            data: {branchGet: branchGet},
            type: 'GET',
            dataType: 'json',
            success: function(data)
            {
                if(data == 'success'){
                    location.reload();
                }
            }
        });
    });
});
</script>

The php page that the second script is sending the information too.

<?php
$user = '';
$pass = '';

try {
$DBH = new PDO('mysql:host=localhost;dbname=nightlineDB;', $user, $pass);
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$DBH->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$branch = $_GET['branchGet'];

$true = 1;

$contactList = $DBH->prepare('SELECT contactName, contactNumber FROM branchcontact WHERE branch = ? AND inUse = ?');
$contactList->execute(array($branch, $true));
$contactResult = $contactList->fetchAll(PDO::FETCH_ASSOC);

<div><p><h4>First Contact</h4> <?php echo $contactResult['0']['contactName'] ." ".$contactResult['0']['contactNumber'] ?></p></div>

$success = 'success';
echo json_encode($success);

}
catch(Exception $e) {
echo 'Error: '.$e->getMessage();
}
?>

The console.log from the second script is showing the correct branch has been selected, however, I get an error stating the the branchGet in the PHP file is and 'Undefined Index' and the script fails. I am not able to query the database as the variable is empty.

Could someone please point me in the right direction as I am a little more then a bit confused now.

Cheers in advance,

Blinky

Upvotes: 0

Views: 357

Answers (1)

Jens Tandstad
Jens Tandstad

Reputation: 109

If you get an error on this line

$branch = $_GET['branchGet'];

it means that the GET variable was not set and therefore no index for this variable exists in the $_GET array which PHP provides as a resource for GET variables.

Get variables are always encoded in the URL. The branch ?branchGet=somevalue to the URL. Unless there is some code that you have not posted, the $_GET variable contains whatever

  1. Open the developer tools in your browser and look at the XHR request. Verify that the URL posted to the php script ends with Bookings.php?branchGet=something If it does, the error is on the PHP side. If not it's on the jquery side.

  2. I cant really see what could be wrong on the PHP, but the error indicates that the request URL was missing ?branchGet=something in the URL.

Upvotes: 1

Related Questions