Bruce Gust
asked on
How do I display <div> if result of query is empty?
I've got an AJAX form dynamic happening. Everything is working just like the client wants with the exception of what happens when a user goes to login and nothing is found in the database.
When the user clicks the "submit" button, here's the code that is called:
Notice the "$('#verify_form').hide()" command.
If nothing is returned, I need that div to be visible.
How?
When the user clicks the "submit" button, here's the code that is called:
$('#findPortalUser').click( function(event){
event.preventDefault();
if (validateForm()) {
$("#verify_form").hide();
var out = {
'usrDOB' : $('#usrDOB').val(),
'usrEEID' : $('#usrEEID').val(),
'cpID' : 574,
'displayLinks' : $('#displayLinks').val()
};
$.post('api_bmcjax.php?p=verify&displayLinks=false&linkType=surveyevents', out, function(data)
{
$('#window').html(data);
});
}
});
Notice the "$('#verify_form').hide()"
If nothing is returned, I need that div to be visible.
How?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I should mention that I'm not looking for a "success" or a "fail" as much as I'm looking for what's coming back from the PHP page.
If I get this returned:
...then I need to NOT hide the input fields.
Anything else, I need to hide them. How do I check for what is coming back from the form submission?
If I get this returned:
<div style="border-radius:10px; margin:auto; width:65%; padding:10px; height:auto; background-color:#006699; color:#ffffff;">
We were unable to match the information you provided.<br><br>Please try again.
</div>
, ...then I need to NOT hide the input fields.
Anything else, I need to hide them. How do I check for what is coming back from the form submission?
Not 100% sure what you are asking - but does this not do what you want?
$('#findPortalUser').click( function(event){
event.preventDefault();
if (validateForm()) {
$("#verify_form").hide();
var out = {
'usrDOB' : $('#usrDOB').val(),
'usrEEID' : $('#usrEEID').val(),
'cpID' : 574,
'displayLinks' : $('#displayLinks').val()
};
$.post('api_bmcjax.php?p=verify&displayLinks=false&linkType=surveyevents', out, function(data)
{
if (data) {
$('#window').html(data);
}
else {
$("#verify_form").show();
}
});
}
});
ASKER
This is the function that the data is being directed to:
You've got several scenarios, but in most of the cases that I'm concerned with, it's the $out variable and the HTML stored within that's coming back. How do I check the content of $out and use that to determine whether or not my form is hidden ($("#verify_form").hide(); )?
function verifyUserImproved(Array $params, $displayLinks, $linkType)
{
global $CN;
if (isset($params['empID'])) {
$params['empID'] = (int) $params['empID'];
}
if (isset($params['cpID'])) {
$params['cpID'] = (int) $params['cpID'];
}
if (isset($params['empPID'])){
$params['empPID'] = (int) $params['empPID'];
}
// possible return values [ Portal, Survey, Events ]
$returns = [
'portal',
'survey',
'events'
];
$out = <<<OUT
<div style="border-radius:10px; margin:auto; width:45%; padding:5px; height:auto; text-align:center;">
<form method="post" style="display:inline-block;">
<table class="table" style="border:none;; width:auto; margin:auto;">
<tr>
<td style="border:none;; font-size:10pt; text-align:center;" colspan="2">
<p class="row text-center"><span style="color: #006699; font-weight:bold; font-size:12pt;">Is this you?</span></p>
{{usrFN}} {{usrLN}} | {{usrCity}} {{usrState}}, {{usrZip}}
</td>
</tr>
<tr style="line-height:25px;">
<td style="text-align:center; border:none;;">
<input type="hidden" name="usrID" id="usrID" value="{{usrID}}">
<input type="hidden" name="usrFN" id="usrFN" value="{{usrFN}}">
<input type="hidden" name="usrLN" id="usrLN" value="{{usrLN}}">
<input type="hidden"name="usrVerified" id="usrVerified" value="{{usrVerified}}">
<input class="btnConfirmYes" type="button" name="verifyUseryes" id="verifyUseryes" value="Yes" style="background-color:#006699; border-radius:10px; margin:auto;
width:75px; padding:5px 10px 10px 10px; height:35px; color:#ffffff; font-size:10pt;">
<input type="button" name="verifyUser" id="verifyUserno" value="No" style="background-color:#006699; border-radius:10px; margin:auto;
width:75px; padding:5px 10px 10px 10px; height:35px; color:#ffffff; font-size:10pt;"></form>
</td>
</tr>
</table>
</div>
OUT;
if (isset($params['test'])) {
$testing = true;
unset($params['test']);
echo $out;
}
$where = '';
$values = [];
if (isset($params['cpID'])) {
$params['E.cpID'] = $params['cpID'];
unset($params['cpID']);
}
if (isset($params['empID'])) {
$params['E.empID'] = $params['empID'];
unset($params['empID']);
}
if (isset($params['empPID'])){
$params['E.empPID'] = (int) $params['empPID'];
unset($params['empPID']);
}
foreach ($params as $key => $val) {
if (end($params) != $val) {
if ($key == 'usrSSN' || $key == 'usrEEID') {
$where .= " $key LIKE ? AND ";
} else {
$where .= " $key = ? AND ";
}
} else {
if ($key == 'usrSSN' || $key == 'usrEEID') {
$where .= " $key LIKE ?";
} else {
$where .= " $key = ?";
}
}
if ($key == 'usrSSN' || $key == 'usrEEID') {
array_push($values, "%{$val}");
} else {
array_push($values, $val);
}
}
try {
$sql = "SELECT
usrID
,E.empID
,usrEEID
,usrFN
,usrLN
,usrDOB
,usrCity
,usrState
,usrZip
,usrUsername
,usrPassword
FROM hraUsers U INNER JOIN Employers E on U.empID = E.empID INNER JOIN ChannelPartners CP on E.cpID = CP.cpID
WHERE $where";
//echo $sql;
$stmt = $CN->prepare($sql);
$stmt->execute($values);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// echo $e->getMessage();
echo "There was an Error verifying this user. If this continue contact the helpdesk.";
}
if (! empty($result)) {
$replace = [
'{{usrID}}',
'{{usrFN}}',
'{{usrLN}}',
'{{usrCity}}',
'{{usrState}}',
'{{usrZip}}',
'{{usrVerified}}'
];
$values = [
$result['usrID'],
$result['usrFN'],
$result['usrLN'],
$result['usrCity'],
$result['usrState'],
$result['usrZip'],
'true'
];
$out .= "<div id='extras' style='display: none'>";
ob_start();
if ($displayLinks == 'true') {
getEventsByEmployerId($result['usrDOB'], $result['usrEEID']);
}
// ONE OFF needs FIXING USED for auto populating sub-survey groups
$subSurveyGroupArray = [
6100,
7015,
7016,
5313
];
$usrID = in_array($result['empID'], $subSurveyGroupArray) ? $result['usrID'] : null;
$survey = findSurvey($result['empID'], $usrID);
$survey_guid = buildGUID(array(
'empSurveyUsername' => $survey['credUsername'],
'empSurveyPassword' => decrypt($survey['credPassIV'], $survey['credPass']),
'usrLang' => '',
'empID' => $survey['empID'],
'hID' => $survey['hID'],
'usrID' => $result['usrID']
));
if ($linkType == 'surveyevents') {
$result['usrID'] = encryptVals($result['usrID']);
$out .= <<<OUT
<script>
$('#verifyUseryes').click(function($event){
event.preventDefault();
$('#li_dob').show();
$.post('api_bmcjax.php?p=d11', {'ui':'{$result['usrID']}', 'ei':'{$result['empID']}'}, function(data){
$('#window').html(data);
});
});
$('#verifyUserno').click(function(){
location.reload();
$('#verify_form').show();
$('#window').hide();
//$('#window').html('');
});
</script>
OUT;
} elseif ($linkType == 'getAllEvents') {
$result['usrID'] = encryptVals($result['usrID']);
$out .= <<<OUT
<script>
$('#verifyUseryes').click(function($event){
//event.preventDefault();
$.post('api.php?p=getAllEvents', {'ui':'{$result['usrID']}', 'ei':'{$result['empID']}'}, function(data){
$('#window').html(data);
});
});
$('#verifyUserno').click(function(){
$('#step_1').show();
$('#window').html('');
});
</script>
OUT;
}
if ($displayLinks == 'none') {
$out .= <<<OUT
<script>
$('#verifyUseryes').click(function(){
window.location='https://www.personalhealthsurvey.net/login.php?{$survey_guid}';
});
$('#verifyUserno').click(function(){
$('#step_1').show();
$('#window').html('');
});
</script>
OUT;
} elseif ($displayLinks == 'portal') {
$portalLink = buildPortalLogin('','', $result['usrID'] );
$out .= <<<OUT
<script>
$('#verifyUseryes').click(function(){
window.location='$portalLink';
});
$('#verifyUserno').click(function(){
$('#step_1').show();
$('#window').html('');
});
</script>
OUT;
} else {
$out .= "<a href='https://www.personalhealthsurvey.net/login.php?{$survey_guid}'>{$survey['hName']}_10</a>";
$out .= ob_get_contents();
$out .= "</div>";
$out .= "<script>$('#verifyUseryes').click( function(){ $('#extras').css('display', 'block'); });
$('#verifyUserno').click(function(){
$('#step_1').show();
$('#window').html('');
});</script>";
}
$out = str_replace($replace, $values, $out);
echo $out;
}
else
{
$out = <<<OUT
<br>
<div style="border-radius:10px; margin:auto; width:65%; padding:10px; height:auto; background-color:#006699; color:#ffffff;">
We were unable to match the information you provided.<br><br>Please try again.
</div>
OUT;
echo $out;
}
}
You've got several scenarios, but in most of the cases that I'm concerned with, it's the $out variable and the HTML stored within that's coming back. How do I check the content of $out and use that to determine whether or not my form is hidden ($("#verify_form").hide();
based on your last post ... my above post won't work
You should consider redesigning your PHP return
Instead of returning the data - return a JSON string
You should consider redesigning your PHP return
Instead of returning the data - return a JSON string
{
status: true | false,
html: <html string here>
}
Now you can do the check in your success function$.post('api_bmcjax.php?p=verify&displayLinks=false&linkType=surveyevents', out, function(data)
{
$('#window').html(data);
if (!resp.status) {
$("#verify_form").show();
}
}, 'JSON');
ASKER
Julian! I saw you were looking at the question and I was furiously pecking away at my keyboard trying to add some more specifics to the question.
It's not so much whether or not I'm getting data, it's the text within that data that I need to evaluate.
How?
It's not so much whether or not I'm getting data, it's the text within that data that I need to evaluate.
How?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
also, check it with fiddler to see if your ajax call is really made...