Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question

marked as duplicate by JasonMArcher, Mario, andrewsi, Divi, Ladlestein Jun 27 at 2:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
You can't call PHP with JavaScript. –  Brett Santore Jun 26 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 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 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. api.jquery.com/jquery.ajax –  mrpotocnik Jun 26 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 at 18:45
show 2 more comments

2 Answers 2

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( ); 
                }); 
    }
share|improve this answer
add comment

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/

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.