Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-07-2013, 10:46 PM   PM User | #1
Jian0203
New Coder

 
Join Date: Mar 2012
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
Jian0203 is an unknown quantity at this point
Weak programming learner needs helps (compare input with database)

I am developing an extremely simple login system for myself, not going to publish it online.
My scenario : During registration, user chooses his username and insert desired click count(s) for login and it will be stored in database. During login, user will first key in his username, then user will be redirect to a page where user will click on an image with onclick function to count the click(s).

Problem : How can I get the "click count(s)" from login.php and then compare with the database recorded clicks in checkclick.php ? I have tried my best to find my own idea to do it for days already. However, i failed. To be honest, i am slow and weak in programming, so i hope i can get some helps here. My friends did suggest me to use AJAX, but after went through the tutorial, i don't really understand. Can anyone please help ? I will be grateful.

login.php
PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 transitional//EN"
        "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd">
    <?php
        
include("connection.php");
        
$username $_SESSION["CID"];
        
$table1=mysql_query("select * from pic_a");
        
$table2=mysql_query("select * from pic_b");
        
$table3=mysql_query("select * from pic_c");
        
        
$tab1[0]=0;
        
$tab2[0]=0;
        
$tab3[0]=0;
        
        
$i=0;
        while(
$row=mysql_fetch_assoc($table1))
        {
            
$tab1[$i]= $row["pic_link"];
            
$i++;
        }
        
        
$i=0;
        while(
$row=mysql_fetch_assoc($table2))
        {
            
$tab2[$i]= $row["pic_link"];
            
$i++;
        }
        
        
$i=0;
        while(
$row=mysql_fetch_assoc($table3))
        {
            
$tab3[$i]= $row["pic_link"];
            
$i++;
        }
        
        
        
$rand1=rand(0,9);
        
$rand2=rand(0,9);
        
$rand3=rand(0,9);
        
        
    
?>
    <script type="text/javascript">
    var count = 0;
    function countClicks() {
    count = count + 1;
    document.getElementById("clicks").innerHTML = count;
    }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link rel="stylesheet" href="css/default.css" type="text/css" media="screen" title="default" charset="utf-8" />
        <title>Registration</title>
        
    </head>
    <body>
        <div id="logo_area">
            <ul>
                <li ><a href="index.php">Home</a></li>
                <li class="nav_line"><a href="login.php">Login</a></li>
                <li class="nav_line"><a href="register.php">Register</a></li>
                <li class="nav_line"><a href="#">Contact</a></li>
            </ul>
        </div>
        
        <div id="wrapper">
            
            <div id="header">
                
            </div>
            
        <div id="content">
    <!-- insert the page content here -->
    <h1>Registration</h1>
            
            <form action = "" name = "allpass" method = "post" enctype = "multipart/form-data">
                
                <p style="font-size:20px">Username: <?php echo $username;?> </p>
                <p style="font-size:20px">Password:</p>
            <p><table border = 0>
            
            <td>
                <div STYLE=" height: 800px; width: 165px; font-size: 12px;">
            <table border="1">
                 <tr id="bird" onclick="countClicks();">
                    <td><img src="password\<?php echo $tab1[$rand1]; ?>" alt="" width="76" height="67"></td>
                    <td>Bird</td>
                 </tr>
                 <tr id="car" onclick="countClicks();">
                    <td><img src="password\<?php echo $tab2[$rand2]; ?>" alt="" width="76" height="67"></td>
                    <td>Car</td>
                 </tr>
                 <tr id="computer" onclick="countClicks();">
                    <td><img src="password\<?php echo $tab3[$rand3]; ?>" alt="" width="76" height="67"></td>
                </table>
                </div>
                </td>
                </table>
                <p id="clicks">0</p>
                <input type = "submit" style=" height: 25px; width: 75px" value = "Login" name = "submitbtn" />
    </form></p>
    </div>
            
            <div id="footer">
                <a href="#">&copy;YourWebSite.com</a> &nbsp;|&nbsp; <a href="http://viminteractive.com">Theme design by VIM</a> &nbsp;|&nbsp; <a href="http://viminteractive.com">OpenGraphicDesign.com</a> &nbsp;|&nbsp; <a href="#">Legal</a>
            </div>
            
        </div>
    </body>
    </html>
checkclick.php
PHP Code:
    <?php
        
include("connection.php");
        
        
    if(!isset(
$_GET["username"]) || !isset($_GET["clicks"]))
    die(
"Error");
        
$username $_GET["username"];
        
$jsClicks $_GET["clicks"];
        
$phpClicks null;
        
        
$data mysql_query("SELECT clicks FROM customerdetails WHERE customer_username='$username'");
    while(
$row mysql_fetch_array($data))
    {
    
$phpClicks $row["clicks"];
    }
        if( 
$phpClicks == $clicks)
        {
        
?>
        <script type="text/javascript">
            alert("success");
            location = "welcome.php";
        </script>
    <?php
    
} else {
    
?>
        <script type="text/javascript">
            alert("fail");
            location = "login.php";
        </script>
        
    <?php
    
}
    
?>
Thank you so much ^.^

Last edited by Jian0203; 01-07-2013 at 10:55 PM..
Jian0203 is offline   Reply With Quote
Old 01-07-2013, 11:28 PM   PM User | #2
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Very simply put ajax just allows you to run a script in the background and grap the results. For example.

//file names here are just for example you can call them anything you want
lets say your running a php file we will call it file1.php
and in that file you call ajax to run file2.php

Lets say that in file2.php (the ajax called file) it outputs "HELLO" to the page. Using an ajax routine allows
you to grab that output of "HELLO" and use that result in your file1.php without having to leave your file1.php.

Hope that helps. Let me see what i can see here, im not a pro by any means but i will see what i can see here.


Ok i took a look and it seems to me your making this way to complicated, if you just want to increment the number of clicks to whatever they click on then keep it simple.

The user logs in.
The user is taken to a page with lets say 3 images image1.jpg image2.jpg image3.jpg
And lets say you have a table named imageclicks and in that table you have three fields set as INT clicks_img1 clicks_img2 clicks_img3

Then instead of clicking on the image itself maybe click on the submit button below it.
In your form you could have submit button, one form under each image.
Or you could set up a radio button display where you can have the same name attributes,
but lets keep this simple for ya. I did it this way to help your wrap your brain around the process is all.

Code:
<form method="post" action="whatever file" name="imgFrm1" >
<input type="hidden" name="img1"  value="1" />
<input type="submit" name="submit1" value="submit" />
</form>

<form method="post" action="whatever file" name="imgFrm2" >
<input type="hidden" name="img2"  value="2" />
<input type="submit" name="submit2" value="submit" />
</form>

<form method="post" action="whatever file" name="imgFrm3" >
<input type="hidden" name="img3"  value="3" />
<input type="submit" name="submit3" value="submit" />
</form>
Now which ever submit button they click on will send that value in the post value.
So now we check the post value

PHP Code:
<?php

if($_POST['img1'])
{
$theyclicked "clicks_img1";  //this is the name of table field we are gonna use it in the query 
}elseif($_POST['img2'])
   {
   
$theyclicked "clicks_img2";  //this is the name of table field we are gonna use it in the query 
   
}else{
             if(
$_POST['img3'])
            {
            
$theyclicked "clicks_img3";  //this is the name of table field we are gonna use it in the query 
             
}
          }
//close else


//then just do your query because now you know which one they clicked and you can add to that value.

$query "UPDATE  imageclicks SET $theyclicked = $theyclicked +1";

//then when you need it just grab the value from the table.
?>

This is not tested but hope it helps you with your project. And there are fancier more elaberate ways of doing this but as i said i just wanted to keep it simple so you could follow the process, then once you get the process in your head you can attack it in a more elaberate way using javascript or ajax or different form attributes.

Last edited by durangod; 01-08-2013 at 12:14 AM.. Reason: made small correction
durangod is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:13 AM.


Advertisement
Log in to turn off these ads.