0

I dont know how to use ajax in my problem: I have a function in php (assign) that update a temporary table in database, I want to when user user click on a button (feedback function that is defined in javascript) this function (assign) run, what should I do?

   <script>

       function feedback(){
            var boxes = document.getElementsByClassName('box');
            for(var j = 0; j < boxes.length; j++){
                if(boxes[j].checked) {
                    assign(1);
                }
                else{
                    assign(0);
                }
            }
        } 

   </script>




  <?php
        $con = mysql_connect("localhost", "root", "")
        or die(mysql_error());   
        if (!$con) { 
            die('Could not connect to MySQL: ' . mysql_error()); 
        } 
        mysql_select_db("project", $con)
        or die(mysql_error());
        $result = mysql_query("select * from words");
        echo "<table border='1'>
           <tr>
              <th>word</th>
              <th>meaning</th>
              <th>checking</th>
            </tr>";
            while($row = mysql_fetch_array($result)) {
                  echo "<tr>";
                    echo "<td>" . $row['word'] . "</td>";
                    $idd= $row['id'] ;
                    echo "<td>". "<div class='hiding' style='display:none'>".$row['meaning']."</div>"."</td>";
                    echo "<td>";
                     echo "<input class=\"box\" name=\"$idd\" type=\"checkbox\" value=\"\"> ";
                    echo "</td>";
                  echo "</tr>";
                  }
         echo "</table>";

                function assign($checkparm){

                      //mysql_query("update words set checking=$checkparm ");
                       mysql_query("create TEMPORARY TABLE words1user1 as (SELECT * FROM words) ");         
                       mysql_query("update words1user1 set checking=$checkparm ");

                                      }

         mysql_close($con);                        
        ?>
        <button onclick="ShowMeanings()">ShowMeanings</button>
        <button onclick="feedback()">sendfeedback</button>  
4
  • possible duplicate of how to call a function in javascript which is defined in php? Commented Oct 11, 2013 at 17:02
  • 2
    If you don't know how to use AJAX, then I strongly suggest you learn how. Other than a full-blown form submission, AJAX is the ONLY way Javascript can invoke PHP code. Commented Oct 11, 2013 at 17:03
  • is it not working now? whats the error? Commented Oct 11, 2013 at 17:08
  • You cant call function assign within javascript as it is a php function.Replace your assign function with a ajax and interact to php via ajax.That is the only way to proceed. Commented Oct 11, 2013 at 17:12

2 Answers 2

6

There is only one way to call a php function after the page is loaded:

1.ajax:

function callPHP() {
    $.ajax ({
        url: "yourPageName.php",
        data: { action : assign }, //optional
        success: function( result ) {
            //do something after you receive the result
        }
    }

in your PHP, write

if ($_POST["action"] == "assign")
{
    assign(your parameters); //You need to put the parameters you want to pass in
                             //the data field of the ajax call, and use $_POST[]
                             //to get them 
}
Sign up to request clarification or add additional context in comments.

Comments

1

There are many great guides on the internet. I will however suggest you get too know JQuery. It will help you on your learning curve.

function ajaxCall(){
$.ajax({
    type: "GET",
    url: "scripts/on/serverside.php"
   });
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.