soulglow1985
soulglow1985

Reputation: 143

Trouble with $.post and mysql db

Someone please help! It's probably a really simple mistake I'm making, but I'm new to PHP & MySQL. I'm trying to retrieve info from the db based on a user's date choice. I'm using some examples I found on youtube to post the input to the php page with jquery and display the retrieved values from php on the html page. However, it seems to only return the else if statement below. Your help is greatly appreciated!

Html Below:

<div id="customDateRange">
    <h2>Welcome <?php echo $companyName ?></h2>
    <h3 style="color:#ff0000">This is currently under development</h3>
    <h4>Please specify the dates you would like to view below:</h4>
    <label for="dateStart">Start Date</label>
    <input class="datePick" id="dateStart" name="dateStart" type="text"/>
    <label for="dateEnd">End Date</label>
    <input class="datePick" ud="dateEnd" type="text"/>
    <input id="getDateRange" type="submit" value="Display Report" />
    <div id="fromDatabase"></div>
</div>

Javascript Below:

$('.datePick').datepicker({ minDate: new Date(2013, 3 - 1, 1), dateFormat: 'yy-mm-dd' });

$('input#getDateRange').on('click', function(){
    var startDate = $('input#dateStart').val();
    if ($.trim(startDate) != '') {
        $.post('../../includes/dateRangeDB.php', function(data, status){
            $('div#fromDatabase').text(data);
        });
    }
});

PHP Below:

<?php 
if (isset($_POST['dateStart']) === true && empty($_post['dateStart']) === false) {
    $query = mysql_query("
        SELECT TotalCost FROM adwords_reporting WHERE CampaignID = 'CAMP020' AND Date IN ('" . mysql_real_escape_string(trim($_POST['dateStart'])) . "')
    ");

    echo "Total Cost: " . (mysql_num_rows($query) !== 0) ? mysql_result($query, 0, 'TotalCost') :  'Date Not Found';
}
else if (isset($_POST['dateStart']) === false) {
    echo "WTF MAN!";
}
else {
    echo "It seems there is an issue";
}
?>

Thank you for your help!

Upvotes: 1

Views: 56

Answers (1)

adeneo
adeneo

Reputation: 318302

You need to actually send the date:

$('input#getDateRange').on('click', function(){
    var startDate = $('input#dateStart').val();
    if ($.trim(startDate) != '') {
        $.post('../../includes/dateRangeDB.php', {dateStart: startDate}, function(data){
            $('div#fromDatabase').text(data);
        });
    }
});

to be able to access it in:

$_POST['dateStart']

Upvotes: 3

Related Questions