Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/154192-updating-mysql-tricky/
Share on other sites

<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/

 

 

}

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.

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.

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.

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')");
?>

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.