-3

I am trying to execute some php code that will update my database. Though, I can't seem to get it to work. I tried using a form but I don't want to redirect as I would have to pass variables to the seperate page. What I have now is a button calling javascript which then calls another php functions but then it doesn't let me do anything else. Any suggestions would be helpful

 <script>
  function result(){
alert("<?php uresult(); ?>");
 }

function uresult(){
  //echo ("<script>console.log('in function')</script>");
if (isset($_POST['submit']) ) { 
  if(!empty( $_POST['comments'] ) ) {
     $update_query = "Update ". $Main_Trainees_Tbl. " ".
                  " Set enabled='$enabled' , comments='$comments', ".
          " comments_date='$date' ".
          " Where email = '$email' ";

    $update_result = mysql_query($update_query) or die(mysql_error());
    header('Location: uteptrainee_data.php');
    exit;
} 
}

 }//end exec
   mysql_close($conn);
  //header('Location: uteptrainee_data.php');
  //exit;  
?>
 <button  onclick="result()" name = "submit" >Update</button>
</html>
  • 1
    You can't call PHP with JavaScript. – Brett Santore Jun 26 '14 at 18:32
  • 5
    Have you looked up jQuery Ajax ? With Ajax you can make calls to another PHP page without refreshing current page and fetch results from it. – GoodSp33d Jun 26 '14 at 18:32
  • 1
    Are you confused with client-side and server-side code, or did you just not format your code clearly enough for us to understand what goes where? – blex Jun 26 '14 at 18:33
  • 2
    It looks like there is confusion on what can actually be done with javascript and what can be done with PHP. Remember, Javascript is a client (browser) scripting language and cannot communicate directly with the server. This is why you cannot call php commands in they way it appears in the code above. @GoodSp33d has a great suggestion in looking up jQuery Ajax. http://api.jquery.com/jquery.ajax/ – mrpotocnik Jun 26 '14 at 18:38
  • what I want is just to update my database with php. I have this page as an update page and once the user clicks the button, I wanted it to update the db then redirect to the original page – user3761203 Jun 26 '14 at 18:45
  • @user3761203 You need to use Ajax to achieve what you want. Check out [this tutorial](http://www.php4every1.com/tutorials/jquery-ajax-tutorial/). If you got stuck some where please feel free to post another question. – GoodSp33d Jun 26 '14 at 18:54
  • On a side note... you may want to do some research on SQL injection. – mrpotocnik Jun 26 '14 at 18:57

2 Answers2

1

PHP occurs on the server level and the button click occurs in the browser. You need to do an Ajax call. Ajax allows your JavaScript to call a PHP page in the background.

Check out this link for examples: http://api.jquery.com/jquery.ajax/

Joey N
  • 328
  • 1
  • 2
  • 7
0

I believe you want to do something like this ( snippet of code from a site im developing )

( using your own selectors and method of grabbing the productID ) but something along these lines sounds like what you are looking for

$( document ).on( 'click', $( '.myclass' ), function()
{
    displayProductInfo( '1' );
}

function displayProductInfo( productID )
    {
        $.ajax({
              url: 'myUrl.php',         
              data: {ProductIDNumber: productID},

              success: function( result )
              {
                  $( '.landingPage' ).html( result );  
              }
            }).done( function()
                { 
                    $( '#gallery' ).pikachoose( ); 
                }); 
    }
PugsOverDrugs
  • 515
  • 1
  • 4
  • 14