Kallmas
Kallmas

Reputation: 97

update DB with onClick

i try to update the status of a messae in my pm's by only clicking the read button on my page. so i found some solution here on this site, but it doesn't really work for me.

i use this ajax before the html button:

     <script type="text/javascript">
function setRead(){
      $.ajax({
        type: "POST",
        url: "update_pm_status.php",
        data: { name: $("select[name='gelesen']").val()},
        success:function( msg ) {
         alert( "Data Saved: " + msg );
        }
       });
  }
</script>

the button is styled css and uses it's own javascript to toggle the content (open/close) so i want to update the status by opening the message

<span class="toggleOpen" onclick="setRead()"><input type="hidden" method="post" name="gelesen" value="<?php echo $row['id']; ?>">lesen</span>

the included site is

<?php
ini_set('include_path', 'inc');
    require("connect.php"); 

        {
                  $id = $_POST['id'];


            $sql = "UPDATE
                    PMS
                    SET
                    gelesen = 'yes'
                    WHERE id = '".mysql_real_escape_string($id)."' AND gelesen = 'no'";
                    mysql_query($sql) OR die("<pre>\n".$sql."</pre>\n".mysql_error());

        }

but i get always the notice of the undefined variable 'id'. how can i give this variable to the included site?


because i have to wait 8 hours before i can reply i edit my post:

thanks for the answer but i still get the message of a undefined index: name so the variable is not postet to my update_pm_staus.php ihave updated my script to this:

<script type="text/javascript">

function setRead(){ $.ajax({ type: "POST", url: "update_pm_status.php", data: { id: $(this).children("[name='gelesen']").val()}, success:function( msg ) { alert( "Data Saved: " + msg ); } }); }

and the update_pm_status.php to this:

    ini_set('include_path', 'inc');
    require("connect.php"); 

            $id = $_POST['name'];

            $sql = "UPDATE
                    PMS
                    SET
                    gelesen = 'yes'
                    WHERE id = '".mysql_real_escape_string($id)."' AND gelesen = 'no'";
                    mysql_query($sql) OR die("<pre>\n".$sql."</pre>\n".mysql_error());

i also changed my html like this in hope to bring it to work.

<span class="toggleOpen" onclick="setRead()" input type="hidden" method="post" name="gelesen" value="<?php echo $row['id']; ?>">lesen</span>

Upvotes: 0

Views: 2034

Answers (2)

Kallmas
Kallmas

Reputation: 97

sorry for the late response. i got the to work. i use

<script type="text/javascript">
function setRead($id){
  $.ajax({
    type: "POST",
    url: "update_pm_status.php",
    data: { id: $id}
   });  }

and the html like this

onclick="setRead(this.id);

thanks for to all helping me out :)

Upvotes: 0

Zac Clancy
Zac Clancy

Reputation: 223

It looks as though you are sending the id correctly, but you are sending it as name. You'd want to update your JavaScript to pass the key:value pair that you're expecting, with the key name that you expect in your PHP.

     <script type="text/javascript">
        function setRead(){
          $.ajax({
            type: "POST",
            url: "update_pm_status.php",
            data: { id: $(this).children("[name='gelesen']").val()},
            success:function( msg ) {
              alert( "Data Saved: " + msg );
            }
          });
        }
     </script>

You'll notice that in the data: {} block of the AJAX request, I've updated your key:value pair to be named id, rather than name. If you do what your key to be named name when POST'ing, then you'd want to update your PHP to look for that:

$id = $_POST['name'];

Upvotes: 1

Related Questions