executing php from link without leaving page
12 posts by 3 authors in: Forums > CMS Builder
Last Post: June 7, 2012 (RSS)
By gkornbluth - May 2, 2012 - edited: May 2, 2012
I'VE ALMOST FOUND THE ANSWER, SEE THE NEXT POST IN THIS THREAD...
Sorry for the lame subject title on this post but I couldn't think of another way to say it.
I’m currently updating the contents of a database field when a visitor to viewer.php (a Galleria image viewer) clicks on one of the links in the following code.
(The links are displayed as part of the information field for each image in the Galleria image viewer.)
<?php foreach ($exhibition_submission_filesRecords as $record): ?>
<?php foreach ($record['uploads'] as $upload): ?>
<a href='http://www.apbc.org/submissions/keep.php?submit=1&num=<?php echo $upload['num'] ?>'><span class='title-text-bold'>CLICK TO KEEP</span></a><br />
<a href='http://www.apbc.org/submissions/remove.php?submit=1&num=<?php echo $upload['num'] ?>'><span class='title-text-bold'>CLICK TO REMOVE</span></a>
<?php endforeach ?>
<?php endforeach ?>
When the visitor clicks on the “remove” link for example, they are taken to a remove.php page which updates the field value to ‘1" with the following code:
<?php mysqlStrictMode(false);
$query = "UPDATE `{$TABLE_PREFIX}uploads` SET
info5 = '1'
WHERE num = '".mysql_escape( $_REQUEST['num'] )."'";
mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$userNum = mysql_insert_id();
?>
And then returned to the original Galleria viewer page with:
<script type="text/javascript">
function redirectIt() {
window.location.replace("http://www.apbc.org/submissions/viewer.php");
}
window.onload = redirectIt;
</script>
THE PROBLEM
Since returning to the Galleria viewer page resets the viewer to image 1, I would prefer the update to be executed without the visitor ever leaving the original Galleria Viewer page.
Any suggestions?
Will I feel silly when I find out how easy it is?
Thanks,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] executing php from link without leaving page
By gkornbluth - May 2, 2012
1) I have no idea if this is a secure approach
(Here’s the ajax_click.js code)
function loadurl(dest) {
try {
// Moz supports XMLHttpRequest. IE uses ActiveX.
// browser detection is bad. object detection works for any browser
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// browser doesn't support ajax. handle however you want
}
// the xmlhttp object triggers an event everytime the status changes
// triggered() function handles the events
xmlhttp.onreadystatechange = triggered;
// open takes in the HTTP method and url.
xmlhttp.open("GET", dest);
// send the request. if this is a POST request we would have
// sent post variables: send("name=aleem gender=male)
// Moz is fine with just send(); but
// IE expects a value here, hence we do send(null);
xmlhttp.send("null");
}
function triggered() {
if ((xmlhttp.readyState == 4) (xmlhttp.status == 200)) {
document.getElementById("ajaxlink").innerHTML = xmlhttp.responseText;
}
}
And here's the basic onclick call
<div id=”ajaxlink” onclick=”loadurl(‘my_page.php’)”>Click Here</div>
2) Because of limitations of the way Galleria handles their code I can’t use double quotes in the onclick call. (NOTE: I’ve tried these variations outside of the Galleria code with the same results)
When I try to use single quotes as shown it doesn’t work.
<div id='ajaxlink' onclick='loadurl('http://www.apbc.org/submissions/keep.php?submit=1&num=1476')'>CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick='loadurl('http://www.apbc.org/submissions/remove.php?submit=1&num=1476')'>CLICK TO REMOVE</div>
Replacing the double quotes with " doesn’t work either.
<div id='ajaxlink' onclick="loadurl('http://www.apbc.org/submissions/keep.php?submit=1&num=1476')">CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick="loadurl('http://www.apbc.org/submissions/remove.php?submit=1&num=1476')">CLICK TO REMOVE</div>
This will work when implemented outside of the Galleria code.
<div id='ajaxlink' onclick=”loadurl('http://www.apbc.org/submissions/keep.php?submit=1&num=1476')”>CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick=”loadurl('http://www.apbc.org/submissions/remove.php?submit=1&num=1476')”>CLICK TO REMOVE</div>
Any ideas appreciated.
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] executing php from link without leaving page
By Jason - May 2, 2012
In terms of security, this approach is fine. You'll need to put all of your security checks in the pages being called (ie, keep.php & remove.php).
For the single quotes, you should be able to use the backslash (\) to escape those characters in your string:
<div id='ajaxlink' onclick='loadurl(\'http://www.apbc.org/submissions/keep.php?submit=1&num=1476\')'>CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick='loadurl(\'http://www.apbc.org/submissions/remove.php?submit=1&num=1476\')'>CLICK TO REMOVE</div>
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] executing php from link without leaving page
By gkornbluth - May 2, 2012
Thanks for the heads up about the security issue.
Escaping the single (or double) quotes does not seem to work, even when on a simple test page.
http://artistsofpalmbeachcounty.org/submissions/viewer18.php
You can see the resulting field value change in:
http://artistsofpalmbeachcounty.org/submissions/results18.php
Jerry Kornbluth
Here's the code. Maybe you'll see something I'm missing:
(The double quotes work in firefox, chrome, and safari but not in ie9
The others don’t work in any browser)
<br /><br />With Double Quotes (Works)<br />
<div id='ajaxlink' onclick="loadurl('http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476')">CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick="loadurl('http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476')">CLICK TO REMOVE</div>
<br /><br />With Apostrophes (NG)<br />
<div id='ajaxlink' onclick='loadurl('http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476')'>CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick='loadurl('http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476')'>CLICK TO REMOVE</div>
<br /><br />With Escaped Apostrophes (NG)<br />
<div id='ajaxlink' onclick=\'loadurl('http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476')\'>CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick=\'loadurl('http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476')\'>CLICK TO REMOVE</div>
<br /><br />With Escaped Double Quotes (NG)<br />
<div id='ajaxlink' onclick=\"loadurl('http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476')\">CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick=\"loadurl('http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476')\">CLICK TO REMOVE</div>
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] executing php from link without leaving page
By zip222 - May 3, 2012 - edited: May 3, 2012
I believe you are escaping the wrong quotes. Try escaping the ones within loadurl() like this...
onclick="loadurl(\'http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476\')">
Re: [zip222] executing php from link without leaving page
By gkornbluth - May 3, 2012
Thanks for your suggestion, although it didn't work out. The problem is that in the Galleria viewer where I'm using it (see code below), I can't use any double quotes at all or they break the viewer. And, the only iteration that seems to work with the AJAX is the one with un-escaped outer double quotes.
Wow, this is frustrating.
Jerry Kornbluth
Here are all the iterations I've tried so far:
DOUBLE QUOTES WORK IN FIREFOX, CHROME, AND SAFARI BUT NOT IN IE9<br />
THE OTHERS DON'T WORK IN ANY BROWSER
<br /><br />With Outer Double Quotes (Works)<br />
<div id='ajaxlink' onclick="loadurl('http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476')">CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick="loadurl('http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476')">CLICK TO REMOVE</div>
<br /><br />With ZIP222 (NG)<br />
<div id='ajaxlink' onclick="loadurl(\'http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476\')">CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick="loadurl(\'http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476\')">CLICK TO REMOVE</div>
<br /><br />With Outer & q u o t ; (NG)<br />
<div id='ajaxlink' onclick="loadurl('http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476')">CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick="loadurl('http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476')">CLICK TO REMOVE</div>
<br /><br />With Inner Escaped Double Quotes (NG)<br />
<div id='ajaxlink' onclick='loadurl(\"http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476\")'>CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick='loadurl(\"http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476\")'>CLICK TO REMOVE</div>
<br /><br />With Apostrophes (NG)<br />
<div id='ajaxlink' onclick='loadurl('http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476')'>CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick='loadurl('http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476')'>CLICK TO REMOVE</div>
<br /><br />With Outer Escaped Apostrophes (NG)<br />
<div id='ajaxlink' onclick=\'loadurl('http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476')\'>CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick=\'loadurl('http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476')\'>CLICK TO REMOVE</div>
<br /><br />With Inner Escaped Apostrophes (NG)<br />
<div id='ajaxlink' onclick='loadurl(\'http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476\')'>CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick='loadurl(\'http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476\')'>CLICK TO REMOVE</div>
<br /><br />With Both Apostrophes Escaped (NG)<br />
<div id='ajaxlink' onclick=\'loadurl(\'http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476\')\'>CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick=\'loadurl(\'http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476\')\'>CLICK TO REMOVE</div>
<br /><br />With Outer Escaped Double Quotes (NG)<br />
<div id='ajaxlink' onclick=\"loadurl('http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476')\">CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick=\"loadurl('http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476')\">CLICK TO REMOVE</div>
<br /><br />With Both Double Quotes Escaped (NG)<br />
<div id='ajaxlink' onclick=\"loadurl(\"http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=1476\")\">CLICK TO KEEP</div><br />
<div id='ajaxlink' onclick=\"loadurl(\"http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=1476\")\">CLICK TO REMOVE</div>
And here's where I'm ultimately going to have to use it (in a Galleria image viewer):
The " break the viewer code.
?php foreach ($exhibition_submission_filesRecords as $record): ?>
<?php foreach ($record['uploads'] as $upload): ?>
<a href="<?php echo $upload['urlPath'] ?>">
<?PHP $upload['info1'] = preg_replace("[\"]", "''", $upload['info1'] ); ?><?PHP $upload['info2'] = preg_replace("[\"]", "''", $upload['info2'] ); ?><?PHP $upload['info3'] = preg_replace("[\"]", "''", $upload['info3'] ); ?><?PHP $upload['info4'] = preg_replace("[\"]", "''", $upload['info4'] ); ?><?PHP $upload['info5'] = preg_replace("[\"]", "''", $upload['info5'] ); ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" data-title="<b><?php echo $upload['info4'] ?></b>" data-description="<?php if ($upload['info1']): ?>Medium: <?php echo $upload['info1'] ?><?php endif ?><?php if ($upload['info2']): ?><br />Dimensions: <?php echo $upload['info2'] ?><?php endif ?><?php if ($upload['info3']): ?><br />Price: $<?php echo $upload['info3'] ?><?php endif ?><br />Image Number: <?php echo $upload['num'] ?><?php $imgnum = $upload['num'] ?><br />Record Number: <?php echo $record['num'] ?><?php $recnum = $record['num'] ?><br />
<div id='ajaxlink' onclick="loadurl('http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=<?php echo $upload['num'] ?>')"><span class='title-text-bold'>CLICK TO KEEP</span></div><br />
<div id='ajaxlink' onclick="loadurl('http://www.artistsofpalmbeachcounty.org/submissions/remove.php?submit=1&num=<?php echo $upload['num'] ?>')"><span class='title-text-bold'>CLICK TO REMOVE</span></div><br />
Submitted By: <?php echo $record['last_name'] ?>" >
</a> <?php endforeach ?>
<?php endforeach ?>
</div>
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] executing php from link without leaving page
By Jason - May 7, 2012
Maybe the best approach would be to simplify your function call by creating functions that only take in a number as a parameter instead of an entire URL.
For example, you could write a jQuery function to "keep" an image like this:
<!-- load jquery library -->
<script type = "text/javascript" src = "cmsAdmin/3rdParty/jquery/jquery1.4.1.js"></script>
<script type = "text/javascript">
function keepSubmission( recordNum ) {
ajaxUrl = "http://www.artistsofpalmbeachcounty.org/submissions/keep.php?submit=1&num=" + escape(recordNum);
$.ajax({
url: ajaxUrl,
}).done(function() {
//add code here if anything needs to happen after the ajax call
});
}
</script>
Then you can call this by just passing in the record number:
example:
<div id='ajaxlink' onclick="keepSubmission(<?php echo $upload['num'];?>)">CLICK TO KEEP</div><br />
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] executing php from link without leaving page
By gkornbluth - May 7, 2012
I'll give it a try
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [gkornbluth] executing php from link without leaving page
By gkornbluth - May 7, 2012
I don't have any idea how you know all you know, or how you keep it all inside your head at the same time, but you've made magic happen yet again.
Thank you,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [Jason] executing php from link without leaving page
By gkornbluth - June 7, 2012
Sorry to have taken so long to test this in IE. (I really don't like IE)
In FireFox, the jQuery function works as planned. Clicking on the "CLICK TO KEEP" or "CLICK TO REMOVE" link automatically passes the appropriate values to the ajaxUrl and changes the field value appropriately.
However, I'm having a bit of an issue getting the jQuery function to work in IE (7-9)
In IE, I can enter the ajaxUrl into the browser and append a record number manually and the field values are changed appropriately, but it doesn't work by clicking on the "CLICK TO KEEP" or "CLICK TO REMOVE" link.
Hope you've got an idea.
Thanks,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php