Pass Multiple arguments onMouseover event handler in HTML - javascript

I would like to know how to pass multiple arguments to the the function with onmouseover event handler in HTML.
Here is the following code in PHP ,
<a href='Page2.php?user=" . $userid . "' target='_blank' onMouseOver=\"writetxt('" . $info "')\" onMouseOut=\"writetxt(0)\">"
I would like to pass 2 arguments to writetxt() function something like this :
writetxt($arg1, $arg2)
<a href='Page2.php?user=" . $userid . "' target='_blank' onMouseOver=\"writetxt('" . $info . "," . $img . "')\" onMouseOut=\"writetxt(0)\">"
Thanks

It should be like...
writetxt('" . $info . "', '" . $img . "');
^ ^

use like this
echo "<a href=\"Page2.php?user=" . $userid ."\" target=\"_blank\" onMouseOver=\"writetxt('" . $info . "','" . $img . "')\" onMouseOut=\"writetxt(0)\">" ;

Related

PHP Escaping double Quote on echo with onclick

Please notice the addslashes($model->subject) part in the code below. If the subject contains single quote, it works fine but if it has double quote, javascript generates error and treats that the onclick method ends on the double quote character. How to solve that error?
$string.=" <a href='#' class='' data-toggle='modal' "
. "onclick=\"to_edit('" . $model->id . "','" . $model->doc_type_level_1 . "','" . $model->doc_type_level_2 . "','" . $model->doc_type_level_3 . "','" . addslashes($model->subject) . "','" . $model->date . "',"
. "'" . $model->date_release . "','" . $model->is_confidential . "'"
. ")\" "
. "data-target='#modal_doc' ><span class='fa fa-pencil'></span></a>";
You should use htmlspecialchars to convert these entities, use following code:
htmlspecialchars($model->subject, ENT_QUOTES);

Assign id within a php echoed statement

in the below php statement I want to assign ID to EmployeeName.I will use the ID tag to search an element in the echoed list by name.Where am I making the mistake ?
<?php while( $toprow4 = sqlsrv_fetch_array( $stmt4) ) {
echo "<div class='parent-div'><span class='rank'>" . $toprow4['rank'] . "</span><span class='name'>" id = ' . $toprow4['EmployeeName'] .' "</span><span class='points'>" . $toprow4['pointsRewarded'] . "</span></div>";
} ?>
Add the id to the div element as an attribute and not as textContent
echo "<div class='parent-div' id='" . $toprow4['EmployeeName'] . "'><span class='rank'>" . $toprow4['rank'] . "</span><span class='name'>" . $toprow4['EmployeeName'] . "</span><span class='points'>" . $toprow4['pointsRewarded'] . "</span></div>";
and try and make that name a valid ID by removing spaces or replace them by -.
You need to check your quotes. If you aren't already, I'd strongly recommend using an editor with code highlighting.
<?php
while ($toprow4 = sqlsrv_fetch_array( $stmt4)) {
$rank = $toprow4['rank'];
$id = $toprow4['EmployeeName'];
$points = $toprow4['pointsRewarded'];
echo "<div class='parent-div'><span class='rank'>$rank</span>";
echo "<span class='name' id='$id'></span><span class='points'>$points<span></div>";
}
?>
Seems like you have a big problem escaping your output string correctly. Perhaps you should to try write it clean by using templates and sprintf() like in this sample.
i did not test the logic of the while statement you have given
<?php
$template = '<div class="parent-div">';
$template .= '<span class="rank">%s</span>';
$template .= '<span class="name">id = %s</span>';
$template .= '<span class="points">%s</span>';
$template .= '</div>';
while($toprow = sqlsrv_fetch_array($stmt4) {
echo sprintf(
$template,
$toprow['rank'],
$toprow['EmployeeName'],
$toprow['pointsRewarded']
)
}
?>
Your code is not clean, so you failed on making correct quotes and single-quotes.

Trying to send two values from php to JS

Im retrieving a value from my db, displays on screen as a 0.
echo "<article>" . $gr_display['status'] . "</article>";
Then when im clicking on this DIV i want to send both status and id to my JS function, edit. The ID of this object is 79
echo "<div onclick='edit( " . $gr_display['status'] . "." . $gr_display['id'] . " )' </div>";
Then the start of my script
function edit(status, id) {
console.log(status, id ); some code later }
But im ending up with the result that id and status is combined into one sett of value, leaving the other one "undefined"
From console log: 0.79 undefined
Please make a clarity between PHP and JavaScript. You need to use the right separator and separate.
echo "<div onclick='edit( " . $gr_display['status'] . ", " . $gr_display['id'] . " )'> </div>";
//-----------------------------------------------------^
Replace . with ,. Also please use > for the opening <div>. You forgot to close your opening div.
you have at typo change the . into, between the 2 variables
echo "<div onclick='edit( " . $gr_display['status'] . "," . $gr_display['id'] . " )'> </div>";
//---------------------------------------------------------^
or better use data-attributes
echo "<div class='edit-element' data-status='" . $gr_display['status'] . "' data-id='" . $gr_display['id'] . "'></div>";
$('.edit-element').click(function(){
console.log($(this).attr('data-status'),$(this).attr('data-id'));
});
replace div with this:-
echo "<div onclick='edit( " . $gr_display['status'] . "," . $gr_display['id'] . " )' </div>";
The problem is causes by this line:
echo "<div onclick='edit( " . $gr_display['status'] . "." . $gr_display['id'] . " )' </div>";
Notice that between the two arguments you put a '.' instead of a ',' (a comma). That way, the second argument in your JS function does not have a value
Your code has two issues
You haven't close opening div tag
Use , to separate two parameters
echo "<div onclick='edit( " . $gr_display['status'] . ", " . $gr_display['id'] . ")'>ddd </div>";
<script type="text/javascript">
function edit(status, id) {
alert(status);
alert(id);
}
</script>

JS sequential calling of function fails

I need to exicute one JS function after another sequencually. I can exicute these functions individually and they work but when I put them in a sequence I only get the openPatient() and the openMessage() does not exicute. My JS functions
function openPatient() {
myRestoreSession();
opener.top.RTop.document.location.href = '../patient_file/summary/demographics.php?set_pid=<?php echo attr($ptid); ?>';
}
function openMessage(messageid) {
myRestoreSession();
document.location.href = 'upload_form.php?messageid=' + messageid;
}
My function call:
echo " onclick=\"openPatient().then(openRequest(" .
"'" . addslashes($postid) . "'," .
"'" . addslashes($v1[1]['type']) . "'" .
"))\">" . text($v1[1]['datetime']) . "</td>\n";
This function call exists in this process:
<?php
// Generate a table row for each pending portal request or message.
// This logic merges requests with messages by date.
$v1 = each($result['list']);
$v2 = each($result['messages']);
while ($v1 || $v2) {
echo " <tr class='detail' bgcolor='#ddddff'>\n";
if (!$v2 || $v1 && $v1[1]['datetime'] < $v2[1]['datetime']) {
$postid = $v1[1]['postid'];
$ptname = patientNameFromLogin($v1[1]['user']);
// Get the portal request data.
if (!$postid) die(xlt('Request ID is missing!'));
$result2 = cms_portal_call(array('action' => 'getpost', 'postid' => $postid));
if ($result2['errmsg']) {
die(text($result2['errmsg']));
}
// Look up the patient in OpenEMR.
$ptid = lookup_openemr_patient($result2['post']['user']);
echo " <td>" . text($v1[1]['user']) . "</td>\n";
echo " <td style='cursor:pointer;color:blue;' onclick=\"openPatient()\">" .text($ptname ) . "</td>\n";
echo " <td style='cursor:pointer;color:blue;'";
echo " onclick=\"openPatient().then(openRequest(" .
"'" . addslashes($postid) . "'," .
"'" . addslashes($v1[1]['type']) . "'" .
"))\">" . text($v1[1]['datetime']) . "</td>\n";
echo " <td>" . text($v1[1]['type' ]) . "</td>\n";
echo " <td align='center'><input type='checkbox' name='form_req_cb[" .
attr($postid) . "]' value='" . attr($postid) . "' /></td>\n";
$v1 = each($result['list']);
}
else {
$messageid = $v2[1]['messageid'];
$ptname = patientNameFromLogin($v2[1]['user']);
echo " <td>" . text($v2[1]['user']) . "</td>\n";
echo " <td>" . text($ptname ) . "</td>\n";
echo " <td style='cursor:pointer;color:blue;'";
echo " onclick=\"openMessage(" .
"'" . addslashes($messageid) . "'" .
")\">" . text($v2[1]['datetime']) . "</td>\n";
echo " <td>" . text($v2[1]['user'] == $v2[1]['fromuser'] ?
xl('Message from patient') : xl('Message to patient')) . "</td>\n";
echo " <td align='center'><input type='checkbox' name='form_msg_cb[" .
attr($messageid) . "]' value='" . attr($messageid) . "' /></td>\n";
$v2 = each($result['messages']);
}
echo " </tr>\n";
}
?>
I am thinking part of the problem may be that openPatient() opens in another window. Perhaps it is loosing focus. Any tips to fix this would be appreciated.
EDIT:
What I have tried and helps is adding return this; to openPatient():
function openPatient() {
myRestoreSession();
opener.top.RTop.document.location.href = '../patient_file/summary/demographics.php?set_pid=<?php echo attr($ptid); ?>';
return this;
}
This then executes the next function but the next function executes too soon. it needs to wait for openPatient() to fully load before executing openMessage(). I have tried adding setTimeout( wait, 1000 ); but then openMessage() does not execute at all.
The solution:
The call:
echo " <td style='cursor:pointer;color:blue;'";
echo " onclick=\"openPatient();setTimeout(function(){openRequest(" .
"'" . addslashes($postid) . "'," .
"'" . addslashes($v1[1]['type']) . "'" .
")}, 2500);\">" . text($v1[1]['datetime']) . "</td>\n";
The functions:
function openPatient() {
myRestoreSession();
opener.top.RTop.document.location.href = '../patient_file/summary/demographics.php?set_pid=<?php echo attr($ptid); ?>';
return this;
}
function openMessage(messageid) {
myRestoreSession();
document.location.href = 'upload_form.php?messageid=' + messageid;
}
Keys to success: return this; and the use of the anonymous function with setTimeout in the call.
Posts that helped:
What does "return this" do within a javascript function?
setTimeout delay not working

Escape Javascript in PHP

I am Trying to escape these darn quotes and stuff in JS. I'm trying to use json_encode. My head hurts from looking at quotes, help???
$list = '';
// some loop here
$message = 'centeredPopup(this.href,"myWindow","500","300","yes")';
$jscode = 'json_encode('.$message.');return false';
$list .= '<p>
<a href="http://www.example.com/something.php?id=' . $id . '" onclick="'
.htmlspecialchars($jscode) . '" >' . $name . '</a></p><br>';
If you want to call json_encode there, you need to do this.
$jscode = json_encode($message).';return false';
You don't even need json_encode for this. You can put this there:
$message = 'centeredPopup(this.href,"myWindow","500","300","yes")';
$jscode = $message.';return false';
$list .= '<p> <a href="http://www.example.com/something.php?id=' . $id . '" onclick="' .htmlspecialchars($jscode) . '" >' . $name . '</a></p><br>';
Try:
$message = 'centeredPopup(this.href,"myWindow","500","300","yes")';
$jscode = json_encode($message);
Otherwise everything else should be OK.

Categories