PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 10th, 2013, 02:01 PM
Varsh Varsh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 15 Varsh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 59 m 40 sec
Reputation Power: 0
Jquery/AJAX form issues

I've been putting a table together with the required data and I'm able to display it as needed. What I'm wanting is this:

At the end of each row are two buttons "yes" and "no". When the user clicks "yes" the div for that form disappears and the form for "no" appears. During the transition Jquery submits a MySQL query and updates the database. There are a few problems I'm having though.
Scenario 1
  • When the user clicks the "yes" button all of the "yes" forms fade out and all of the "no" forms fade in on all rows
  • The database does not update
Scenario 2
  • When the user clicks the "yes" button the "no" form does not fade in when the "yes" form should also fade out but doesn't
  • The database does not update
When trying out the SQL query everything is running as it should with no problems at all including error checking, but for some reason nothing happens when it's used as an URL in JQuery/AJAX. The MySQL query is the following:
PHP Code:
<?php require_once($_SERVER['DOCUMENT_ROOT'] . "/includes/headers.php"); ?>
<?php
    
if (($_POST["user_id"]) && ($_POST["game_id"])) {
        
$user $_POST["user_id"];
        
$game $_POST["game_id"];
        
        try {
            
$sql "
                SELECT game
                FROM site_games_owned
                WHERE user = " 
$user " AND game = " $game "
            "
;
            
$result $pdo->query($sql);
            
$count    $result->rowCount();
        }
        catch (
PDOException $e) {
            
$error "Cannot retrieve the owned games before insertion.<br>Error: " $e -> getMessage();
            echo 
$error;
            exit();
        }
        if (
$count != 0) {
            echo 
$added "You have already added that game.";
        } else {
            try {
                
$sql "
                    INSERT INTO site_games_owned SET
                    user = :user,
                    game = :game
                "
;
                
                
$s $pdo->prepare($sql);
                
$s->bindValue(":user"$user);
                
$s->bindValue(":game"$game);
                
$s->execute();
            }
            catch (
PDOException $e) {
                
$error "Cannot add the owned game.<br>Error: " $e -> getMessage();
                echo 
$error;
                exit();
            }
        }
    }
?>

There are no problems as far as I'm aware of with the above PDO query and it currently works anyway. The problem is the Jquery, for Scenario 1 I have the following:
PHP Code:
$(function() {
    $(
".ownBtn").click(function () {
        var 
own = $("own").val();
        var 
user_id = $("user_id").val();
        var 
game_id = $("game_id").val();
        var 
dataString "own=" own "&user_id=" user_id "&game_id=" game_id;
        
        $.
ajax ({
            
type"POST",
            
url"game_own.php",
            
datadataString,
            
success: function() {
                $(
".owned").fadeOut(function() {
                    $(
".owned2").fadeIn();
                })
            }
        });
        return 
false;
    });
}); 

And for Scenarion 2 I have the following:
PHP Code:
$(function() {
    $(
".ownBtn").click(function () {
        var 
own = $("own").val();
        var 
user_id = $("user_id").val();
        var 
game_id = $("game_id").val();
        var 
dataString "own=" own "&user_id=" user_id "&game_id=" game_id;
        
        $.
ajax ({
            
type"POST",
            
url"game_own.php",
            
datadataString,
            
success: function() {
                $(
".owned" game_id).fadeOut(function() {
                    $(
".owned2" game_id).fadeIn();
                })
            }
        });
        return 
false;
    });
}); 

And the form is as follows for Scenario 1:
PHP Code:
<div class="owned2" style="display:none;">fdhskjfhsdk</div>
<
div class="owned">
    <
form>
        <
input type="hidden" name="own" value="1">
        <
input type="hidden" name="user_id" value="<?php echo $vbulletin->userinfo["userid"]; ?>">
        <
input type="hidden" name="game_id" value="<?php echo $games["id"]; ?>">
        <
input type="submit" value="No" class="ownBtn">
    </
form>
</
div


And the form is as follows for Scenario 2:
PHP Code:
<div class="owned2<?php echo $games["id"]; ?>" style="display:none;">fdhskjfhsdk</div>
<
div class="owned<?php echo $games["id"]; ?>">
    <
form>
        <
input type="hidden" name="own" value="1">
        <
input type="hidden" name="user_id" value="<?php echo $vbulletin->userinfo["userid"]; ?>">
        <
input type="hidden" name="game_id" value="<?php echo $games["id"]; ?>">
        <
input type="submit" value="No" class="ownBtn">
    </
form>
</
div

Any ideas where I might be going wrong?

Reply With Quote
  #2  
Old January 13th, 2013, 09:16 AM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 43 m 47 sec
Reputation Power: 6991
The best way to debug a problem like this is to use a tool for inspecting HTTP requests like Firebug, HTTPFox or Fiddler. You can examine the exact parameter values that are being submitted and the exact data that is being returned by your PHP script. This should help indicate where the problem lies.
__________________
PHP FAQ
How to program a basic, secure login system using PHP

Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #3  
Old January 14th, 2013, 12:08 PM
hdewantara's Avatar
hdewantara hdewantara is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Location: Jakarta, Indonesia.
Posts: 149 hdewantara User rank is Sergeant Major (2000 - 5000 Reputation Level)hdewantara User rank is Sergeant Major (2000 - 5000 Reputation Level)hdewantara User rank is Sergeant Major (2000 - 5000 Reputation Level)hdewantara User rank is Sergeant Major (2000 - 5000 Reputation Level)hdewantara User rank is Sergeant Major (2000 - 5000 Reputation Level)hdewantara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 18 h 50 m 40 sec
Reputation Power: 30
Send a message via MSN to hdewantara
Probably small mistakes,
"own", "user_id" and "game_id" are not tags, classnames nor ids but just HTML input's name-attributes. How do you jQuery that? Like the following?
PHP Code:
$(".ownBtn").click(function () {
    var 
own = $('[name="own"]').val();
    var 
user_id = $('[name="user_id"]').val();
    var 
game_id = $('[name="game_id"]').val(); 
    ...
  } 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Jquery/AJAX form issues

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap