Passing multiple variables via ajaxUrl

6 posts by 2 authors in: Forums > CMS Builder
Last Post: October 24, 2017   (RSS)

By gkornbluth - October 9, 2017 - edited: October 9, 2017

Hi All,

I’ve been using some simple ajax code suggested a while ago by Jason from Interactive Tools to pass a url and one parameter ($num) to a page which updates an upload info field with new data. (Post # 221602)

I now need to pass both the $num variable and a second parameter ($newvalue) to the update page using ajax, and I can’t seem to get the code to work.

Hope someone can take a look....

Here’s my attempt at revising the ajax code:

script type = "text/javascript" src = "cmsAdmin/3rdParty/jquery/jquery1.4.1.js"></script>
<script type = "text/javascript">
      function removeSubmission( recordNum ) {
    ajaxUrl = "remove.php?submit=1&num=" + escape(recordNum)+"&newValue=" + escape(newValue);
      
    $.ajax({
    url: ajaxUrl,
    }).done(function() {
        //add code here if anything needs to happen after the ajax call
       alert("Submission Removed");
     });   
      }
  </script>

Here’s how I retrieve the old info5 contents, update it, and and set up 2 variables:

<?php $oldValue = $upload['info5'] ?>
         <?php $newValue = preg_replace('/[^A-Za-z/', '', $oldValue) ?>
         <?php $newValue = "removed" ?>
        <?php $a = $upload['num'] ?>
        <?php $b = $newValue ?>       

And here’s my attempt at a revised link that’s clicked on to initiate the ajax code:

<div id='ajaxlink' onclick="removeSubmission(<?php echo $a, $b ?>)">CLICK TO REMOVE</div>

And here’s the revised MySQL update code page including the second variable ($newValue:)

<?PHP $newValue = mysql_escape( $_REQUEST['newValue']) ?>
<?php mysqlStrictMode(false);  
      $query = "UPDATE `{$TABLE_PREFIX}uploads` SET
       info5  =  $newValue
                 WHERE num = '".mysql_escape( $_REQUEST['num'] )."'";
      mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
      $userNum = mysql_insert_id();
      
   ?>

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - October 16, 2017

Hi Jerry, 

Fort he ajax function, you want to update it to take a second parameter: 

removeSubmission( recordNum, newValue ) {

And for your onclick code you need to quote the values

 onclick="removeSubmission(<?php echo "'$a', '$b'" ?>)">

Which should output something like this: 

 onclick="removeSubmission('123', 'removed')">

And the MySQL code looks good at first glance.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

Hi Dave,

I’ve tried to implement your suggested changes. There must be something I don’t understand because the ajax alert pops up, which I assume means that the ajax script is working, and passing something to the remove.php page, there are no errors in the error log, but the database doesn’t get updated.

I’ve tried to simplify the code as much as I can, but can’t get it to work, and I don’t know how to test to see what variables are actually being passed. Could I impose on you to take a look again?

Thanks,

Jerry Kornbluth

Here’s the modified code:

Ajax script:

<script type = "text/javascript">
      function removeSubmission(recNum, newValue ){
    ajaxUrl = "remove.php?submit=1&recNum=" + escape(recNum)+"&newValue=" + escape(newValue);
      
    $.ajax({
    url: ajaxUrl,
    }).done(function() {
        //add code here if anything needs to happen after the ajax call
       alert("Submission Removed");
     });   
      }
  </script>

Set Variables:

  <?php $newValue = "removed" ?>
        <?php $recNum = $upload['num'] ?>

on click call div:

<div id='ajaxlink' onclick="removeSubmission(<?php echo "'$recNum', '$newValue'" ?>)">CLICK TO REMOVE</div>

MySQL script (remove.php)

<?php $newValue = mysql_escape($_REQUEST['newValue']) ?>
<?php mysqlStrictMode(false);  
      $query = "UPDATE `{$TABLE_PREFIX}uploads` SET 
       info5  =  $newValue
                 WHERE num = '".mysql_escape( $_REQUEST['recNum'] )."'"; 
      mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n"); 
      $userNum = mysql_insert_id(); 
      
   ?> 

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - October 24, 2017

Hi Jerry, 

If you're still having troubles with this could you email me details at dave@interactivetools.com and I can take a look.  Cheers!

Dave Edis - Senior Developer
interactivetools.com

Thanks Dave,

I will take you up on your generous offer.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php