The Midnighter Posted April 15, 2009 Share Posted April 15, 2009 I'm trying to create a form that updates a MySQL record when the user clicks a LINK, not a button. I don't want to open another page for this. There really isn't a need for me to post my code right now, as I'm trying to figure out how to do this method properly. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/ Share on other sites More sharing options...
Mchl Posted April 15, 2009 Share Posted April 15, 2009 Without opening a new page? That would probably require sending an AJAX request. Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810557 Share on other sites More sharing options...
The Midnighter Posted April 15, 2009 Author Share Posted April 15, 2009 Without opening a new page? That would probably require sending an AJAX request. Are you sure? I could have sworn there was a way to create your own function and call it within the page. Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810560 Share on other sites More sharing options...
nadeemshafi9 Posted April 15, 2009 Share Posted April 15, 2009 <a href='javascript: postformajax();'>updatedb</a> function postformajax(){ $.ajax({ type: "POST", url: "some.php", data: "name=John&location=Boston", /// you will need somthing like form.feild.value for this string success: function(msg){ alert( "Data Saved: " + msg ); // here you can put a result into the current page dom eg into a div } }); you will need to include jquery in your head http://jquery.com/ } Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810561 Share on other sites More sharing options...
The Midnighter Posted April 15, 2009 Author Share Posted April 15, 2009 Wow, you guys.. this is getting far too complex. I know of a way to do this without any of that bull, it just looks ugly and I was sure there was a better way to do it. <th><a href="#" onclick="<?php $sql = "UPDATE `maxelectronics_ca`.`tables2` SET `END USER` = 'Marenertest' WHERE `tables2`.`ID` =1 LIMIT 1 ;"; $query=mysql_query($sql); ?>" >Update</a></th> This works just fine. Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810564 Share on other sites More sharing options...
Mchl Posted April 15, 2009 Share Posted April 15, 2009 How can this possibly work? PHP is executed on server you know... Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810587 Share on other sites More sharing options...
The Midnighter Posted April 15, 2009 Author Share Posted April 15, 2009 Go try it. Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810593 Share on other sites More sharing options...
mrMarcus Posted April 15, 2009 Share Posted April 15, 2009 I was sure there was a better way to do it.there is a better way .. as was stated above. the way you did it, the PHP is not going to wait for the link to be clicked to run .. anytime that page is opened, the php will execute .. so, your way doesn't work just fine. as mentioned, an AJAX call is the way to go .. jQuery or MooTools. Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810597 Share on other sites More sharing options...
The Midnighter Posted April 15, 2009 Author Share Posted April 15, 2009 the way you did it, the PHP is not going to wait for the link to be clicked to run .. anytime that page is opened, the php will execute .. so, your way doesn't work just fine I guess that would depend if you were clever enough to have an update variable checked on the load of the page. Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810605 Share on other sites More sharing options...
Mchl Posted April 15, 2009 Share Posted April 15, 2009 Why don't you display the source of the page in the browser, and see if there is anything in the onClick="" Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810608 Share on other sites More sharing options...
The Midnighter Posted April 15, 2009 Author Share Posted April 15, 2009 Why don't you display the source of the page in the browser, and see if there is anything in the onClick="" That's what I did ;-) Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810609 Share on other sites More sharing options...
mrMarcus Posted April 15, 2009 Share Posted April 15, 2009 Why don't you display the source of the page in the browser, and see if there is anything in the onClick="" That's what I did ;-) and what did you find? Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810618 Share on other sites More sharing options...
redarrow Posted April 15, 2009 Share Posted April 15, 2009 try this way as a example. <?php echo "<a href='{$_SERVER['PHP_SELF']}?update=".md5('database')."'>update database</a>"; if(isset($_GET['update'])&& $_GET['update']==md5("database")){ echo "<br> <br> DATABASE UPDATED! <br><br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810619 Share on other sites More sharing options...
mandred Posted April 15, 2009 Share Posted April 15, 2009 I'm trying to create a form that updates a MySQL record when the user clicks a LINK, not a button. I don't want to open another page for this. There really isn't a need for me to post my code right now, as I'm trying to figure out how to do this method properly. Thanks in advance. onclick="this.form.submit()" if you don't want the page to reload, use ajax: ajax.js: function ajaxfunction() { var ajaxrequest; try { ajaxrequest = new XMLHttpRequest(); } catch (e) { try { ajaxrequest = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { try { ajaxrequest = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) { alert('Browser does not support HTTP requests.'); return false; } } } return ajaxrequest; } function statechanged() { if (htmlrequest.readyState == 4) document.getElementById('rateresult').innerHTML = htmlrequest.responseText; } function doajax(id, rating) { htmlrequest = ajaxfunction(); if (htmlrequest == null) { alert ('Browser does not support HTTP requests.'); return; } var url = 'http://www.mysite.com/dosql.php'; var params = 'id=' + id + '&rating=' + rating; htmlrequest.open('POST', url, true); htmlrequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); htmlrequest.setRequestHeader('Content-length', params.length); htmlrequest.setRequestHeader('Connection', 'close'); htmlrequest.onreadystatechange = statechanged; htmlrequest.send(params); } dosql.php: <?php $id = $_POST['id']; $rating = $_POST['rating']; mysql_query("INSERT INTO table (id, rating) VALUES ('$id', '$rating')"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810652 Share on other sites More sharing options...
premiso Posted April 15, 2009 Share Posted April 15, 2009 Wow, you guys.. this is getting far too complex. I know of a way to do this without any of that bull, it just looks ugly and I was sure there was a better way to do it. <th><a href="#" onclick="<?php $sql = "UPDATE `maxelectronics_ca`.`tables2` SET `END USER` = 'Marenertest' WHERE `tables2`.`ID` =1 LIMIT 1 ;"; $query=mysql_query($sql); ?>" >Update</a></th> This works just fine. Just to be clear for future on lookers. This will execute the SQL each time the page is loaded. Not because the link was "clicked". So of course it "looks" like it was dynamic and work. But of course it worked due to the simple fact it always run's on page load. That is not executed cause the user clicked the update link. This is what mchl was getting at. Your code is flawed in that clicking that link would not "re-execute" that code. Not to mention that code you put there has no way to be dynamically generated, give that you did not use variables inside the SQL to allow so. Sorry, that was just bothering me PHP - Hypertext Pre-Processor, meaning all the processing is done before the output is displayed. Only with Javascript making a callback to the server using Ajax would that be possible and make it "look" like it is doing it but really it is making a call back to the server without doing a page reload. Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-810658 Share on other sites More sharing options...
nadeemshafi9 Posted April 21, 2009 Share Posted April 21, 2009 Wow, you guys.. this is getting far too complex. I know of a way to do this without any of that bull, it just looks ugly and I was sure there was a better way to do it. <th><a href="#" onclick="<?php $sql = "UPDATE `maxelectronics_ca`.`tables2` SET `END USER` = 'Marenertest' WHERE `tables2`.`ID` =1 LIMIT 1 ;"; $query=mysql_query($sql); ?>" >Update</a></th> This works just fine. wth Quote Link to comment https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/#findComment-815606 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.