开发者

inserting table row data upon onclick checkbox PHP and Ajax and MySQL

开发者 https://www.devze.com 2023-03-30 02:20 出处:网络
I have read similar topics, yet could not finish my code. Short is: you tick a checkbox and this unchains an Ajax response that calls a function. This function gets the ID of the logged in user. Once

I have read similar topics, yet could not finish my code.

Short is: you tick a checkbox and this unchains an Ajax response that calls a function. This function gets the ID of the logged in user. Once I get the ID of the user, I am alright with the PHP script.

I have rewritten the code snippet; haven't tried it yet. Could say this is closer I hope.

I am using 2 scripts, 1 for the Ajax and the other the PHP.

Ajax:

    <html>
    <head>
    <script type="text/javascript">
    function getLoggedInUser()
    {

//IF THERE IS NOTHING CHECKED, THAT IS, STR == 0, THEN REMOVE WHATEVER WAS WRITTEN BEFORE

    if (str=="")
      {
      document.getElementById("txtHint").innerHTML="";
      return;
      } 



    // JUST CHECKING BROWSER COMPATIBILITY ISSUES FIRST

          if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
          }


          else

          {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }


// IF THE SERVER HAS THE RESPONSE READY

          xmlhttp.onreadystatechange=function() 
          {
             if (xmlhttp.readyState==4 && xmlhttp.status==200)
            { 

     document.getElementById("txtHint").innerHTML=xmlhttp.responseText;


        }
      }

THEN ON HAVING TICKED THE CHECKBOX IT SENDS TO THE GETLOGGED IN FN THE USER ID, AND THIS FUNCTION FORWARDS IT TO THE PHP SCRIPT CALLED INSERT USER DATA, WHICH WILL DO JUST THAT.

xmlhttp.open("GET","insertUserData.php?$user="+str,true);


     xmlhttp.send();
  }


</script>
</head>


<body>


<?php $user = getUserId();?>

<form>
<input type="checkbox" name="asset" value="" onclick="getLoggedInUser($user)" /> I am potentially 

interested in this product or service<br />
<div id="txtHint"><b>Note:</b></div>


</form>
<br />

</body>
</html> 

======AND HERE THE PHP SCRIPT insertUserData.php ===========

 <?php

 $user = $_GET['user'];


    // But now, as I indicated, we want the data corresponding to that ID



     $con = mysql_connect('localhost', 'peter', 'abc123');
        if (!$con)
          {
          die('Could not connect: ' . mysql_error());
          }

        mysql_select_db("ajax_demo", $con);


        $query = "SELECT name, country
                  FROM profiles
                  WHERE id = '". $user . "';

        $result = @mysql_query ($sql);

        $row = mysql_fetch_a开发者_Go百科rray($result, MYSQL_ASSOC);

        $name =  ($row['name']);
        $country =  ($row['country']);

        mysql_query("INSERT INTO Persons (name, country)
        VALUES ($name, $country)");
        echo 'Your interest has been saved';

        mysql_close($con);
        ?> 

Thank you in advance


at least this:

$user = getUserId(); 
xmlhttp.open("GET","insertUserData.php?$user="true);

should be changed to :

<? $user = getUserId(); ?> 
xmlhttp.open("GET","insertUserData.php?user=$user", true);

it's not a straigh answer to your question but at least a starting point, also, make sure you enable errors by adding the following code at the top of your php file.

ini_set('display_errors', 1);
error_reporting(E_ALL);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号