0

I am trying to compare the text that is written in the input field with the "UserID" in the database to know where to add 1 pts on the button click. Adding a new user to the DB is not a problem, but then when I want to "UPDATE" the DB nothing happends, it seems like I cant get the $_POST['UserID']; to work in my addPts.php ? Here is an example from my code:

blabla.html

<html>
<head>
    <script type="text/javascript" src="blabla.js"></script>
</head>
<body>

<nav>
<li>
    <form action="addToDB.php" method="post" target="hidden-form">
        <input type="text" id="UserID" name="UserID" placeholder="Your ID Here" style="width:290px"/>
        <input type="submit" id="submit" name="submit" value="Add to DB"/>
    </form>
</li>
</nav>

<section id="mainframe">
        <iframe src="blabla2.html" name="myIFRAME"></iframe>
</section>
<iframe style="display:none" name="hidden-form"></iframe>

<button><a id="theLink" href="http://www.stackoverflow.com" target="myIFRAME">Press me for 1pts</a></button>

</body>
</html>

blabla2.html

<html>
<head>
</head>
<body>
Start message in iFrame
</body>
</html>

blabla.js

$(function (){
        $('#theLink').click(function(){
            var request = $.ajax({
                                    type: "POST",
                                    url: "../bla/addPts.php"                               
                                  });
                                  request.done(function( msg ) {

                                        alert('1 Point added');
                                        return;

                                  });
                                  request.fail(function() {
                                        alert( "Request failed.. " );
                                    });
        });
});

addToDB.php

<?php
$localhost = "localhost";
$dbuser = "blabla";
$dbpass = "blablabla";
$dbname = "bla";

$connect = mysql_connect($localhost, $dbuser, $dbpass);
mysql_select_db("$dbname", $connect);

$UserID = $_POST['UserID'];
$checkIDLenght = strlen($UserID);
$messageNewMember = "WELCOME! New User!";
$messageOldMember = "Welcome back!";
$insert = 'INSERT into membersBLA(UserID) VALUES("'.$UserID.'")';

$query = mysql_query("SELECT UserID FROM membersBLA WHERE UserID='".$UserID."'");

    if (mysql_num_rows($query) != 0)
    {
    echo "<script type='text/javascript'>alert('$messageOldMember');</script>";
    }
    else {
    mysql_query($insert);
    echo "<script type='text/javascript'>alert('$messageNewMember');</script>";
    }
?>

addPts.php

<?php
$localhost = "localhost";
$dbuser = "blabla";
$dbpass = "blablabla";
$dbname = "bla";

$connect = mysql_connect($localhost, $dbuser, $dbpass);
mysql_select_db("$dbname", $connect);

$user = $_POST['UserID'];
mysql_query("UPDATE membersBLA SET UserPts = UserPts + 1 WHERE UserID='".$user."'");
?>

It works to "UPDATE" the DB if I type in the " WHERE UserID='Name'"); " manually, then it update the "UserPts" for the name I wrote.. but Getting the typed name from the input to compare does not seems to work?

fool-dev
  • 7,671
  • 9
  • 40
  • 54
  • You're not sending any POST data with your ajax request. Also [don't use mysql_*](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Marvin Dec 27 '15 at 11:49

1 Answers1

1

Try this //Retrieve value of userid and use data paremeter of jQuery.ajax()

$(function (){
        $('#theLink').click(function(){

          var UserID=jQuery("#UserID").val();
            var request = $.ajax({
                                    type: "POST",
                                    url: "../bla/addPts.php",
                                    data:{'UserID': UserID},
                                  });
                                  request.done(function( msg ) {

                                        alert('1 Point added');
                                        return;

                                  });
                                  request.fail(function() {
                                        alert( "Request failed.. " );
                                    });
        });
});
Domain
  • 11,562
  • 3
  • 23
  • 44