Bringing data from html to php and posting it onto database -


<img id="image" src="jj.png" onclick="randomizeimage();"/> <h2 id="score" name="score1">0</h2>  <script>     var counter=0;     function randomizeimage() {         counter++;         var test= document.getelementbyid('score');         test.innerhtml= counter;     } </script>  <?php     $con = mysql_connect("localhost","root");     if (!$con)     {         die('could not connect: ' . mysql_error());     }      mysql_select_db("database", $con);      $user_name = $_cookie["username"]; //i stored username cookie , retrieved here      echo $user_name; //echo can check if right      $new= $_post["score"]; //**this part undefined**      $sql = "update user set score = '$new' id='$user_name'";      if (!mysql_query($sql,$con))     {         die('error please try again ' . mysql_error());     }      mysql_close($con)  ?> 

i have image whenever click on it, call function increase counter 1. score reflected on html side , increase whenever click on image. want bring counter data php can upload onto database matches user's username he/she entered in previous phpfile. cant bring value of score over? keeps saying undefined index.

i have 2 colums called id , score. id score user's username , score score counter. want score updated everytime image pressed respect username.

database name :database table name is: user

is there anyway without ajax?

this how can send ajax request server. make sure include jquery first.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>   <script>     var counter=0;     function randomizeimage() {       counter++;       var test= document.getelementbyid('score');       test.innerhtml= counter;        $.ajax       (           "ajax.php",           {               method: "post",               data: { score: counter}           }       );     }   </script> 

next have put php code separate file, call "ajax.php".

<?php      $score = filter_input(input_post, "score");     if (isset($score) && $score != "") {      $con = mysql_connect("localhost","root");     if (!$con)     {        die('could not connect: ' . mysql_error());     }     mysql_select_db("database", $con);     $user_name = $_cookie["username"]; //i stored username cookie , retrieved here      echo $user_name; //echo can check if right     $sql = "update user set score = '$score' id='$user_name'";    $result =mysql_query($sql) or die("could not update" . mysql_error);    if (!mysql_query($sql,$con))   {     die('error please try again ' . mysql_error());   }   mysql_close($con)   }  ?> 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -