9 Replies - 630 Views - Last Post: 06 April 2013 - 04:17 AM

#1 Timeflies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-February 13

Problem understanding Ajax/Javascript?

Posted 05 April 2013 - 01:26 PM

This is the first time I have used PHP and Ajax and I have been working on this very basic assignment to understand PHP, Ajax, and Javascript a little better but I really am not sure how it works yet so I am a little overwhelmed by it.

There is a login which asks for a username and password, if the user is a customer it just returns transactions, and if the user is a teller it allows them to create a transaction(debit or credit)

Bankrid is what determines who the customer is(to return transactions) or for the teller where the transaction made will go.

All of the PHP code is complete(I think?) and I just need to figure out what to do with the javascript and Ajax. I'm used to just having a simple HTML page and using Javascript to do some math, or create a little card game. I'm not asking anyone to do this for me just some guidance on how to do it and the steps I need to work on. Thanks for any help!

Some starter scripts given

login.php
<?php

$con = mysql_connect("(taken out)", "(taken out)", "(taken out");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }



$userid = $_POST['userid'];

$password = $_POST['password'];



  

$db_selected = mysql_select_db("Database",$con);

$sql = "SELECT * from user where userid='{$userid}' and password='{$password}'";



$result = mysql_query($sql,$con);



if($row = mysql_fetch_array($result)) {

    echo $row['bankrid'] . ":" . $row['usertype'];

}

else

{

    echo "failure";

}



mysql_close($con);

?>


getTransaction.php
<?php

$con = mysql_connect("(taken out", "(taken out)", "(taken out)");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }



$bankid = $_GET['bankid'];

  

$db_selected = mysql_select_db("Database",$con);

$sql = "SELECT * from transaction where bankrid=" . $bankid;



$result = mysql_query($sql,$con);



while($r = mysql_fetch_assoc($result)) {

    $rows[] = $r;

}



echo json_encode($rows);



mysql_close($con);

?>


createTransaction.php
<?php

$con=mysqli_connect("(taken out)","(taken out)","(taken out)","(taken out)");

// Check connection

if (mysqli_connect_errno())

{

  echo "Failed to connect to MySQL: " . mysqli_connect_error();

}



$type = $_POST['type'];

$amount = $_POST['amount'];

$bankrid = $_POST['bankrid'];



$result = mysqli_query($con,"INSERT INTO transaction (type, amount, bankrid)

VALUES ('$type', $amount, $bankrid)");



if($result){

    echo "success";

}

else

{

    echo "failure";

}



mysqli_close($con);

?>


and then ways to test it

loginTester.html
<!doctype html>

<html lang="en">

<head>

    <meta charset="utf-8">

    <title>Login</title>

    <!--[if lt IE 9]>

    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>

    <![endif]-->

    <link rel="stylesheet" href="css/styles.css">

</head>

<body>

    <form action="login.php" method="post" id="tester">

        <fieldset>

            <legend>Login</legend>

            <div><label for="userid">userid</label><input type="textbox" name="userid" id="userid" required></div>

            <div><label for="password">password</label><input type="password" name="password" id="password" required></div>

            <div><label for="submit"></label><input type="submit" value="Login &rarr;" id="submit"></div>

        </fieldset>

    </form>

</body>

</html>


createTransactionTester.html
<!doctype html>

<html lang="en">

<head>

    <meta charset="utf-8">

    <title>Login</title>

    <!--[if lt IE 9]>

    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>

    <![endif]-->

    <link rel="stylesheet" href="css/styles.css">

</head>

<body>

    <form action="createTransaction.php" method="post" id="tester">

        <fieldset>

            <legend>Login</legend>

            <div><label for="type">type</label><input type="textbox" name="type" id="type" required></div>

            <div><label for="amount">amount</label><input type="textbox" name="amount" id="amount" required></div>

            <div><label for="amount">bankrid</label><input type="textbox" name="bankrid" id="bankrid" required></div>

            <div><label for="submit"></label><input type="submit" value="Login &rarr;" id="submit"></div>

        </fieldset>

    </form>

</body>

</html>


Is This A Good Question/Topic? 0
  • +

Replies To: Problem understanding Ajax/Javascript?

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3734
  • View blog
  • Posts: 13,061
  • Joined: 12-December 12

Re: Problem understanding Ajax/Javascript?

Posted 05 April 2013 - 02:42 PM

I read your opening paragraphs a few times but I still cannot see a question..? What is your question? Do you receive any errors?

If you are just starting out with PHP why are you using the [i]deprecated[/il] mysql library? See my signature. But half-way through you switch to the mysqli library. You cannot (or should not) jump between these two.

This post has been edited by andrewsw: 05 April 2013 - 02:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 Timeflies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-February 13

Re: Problem understanding Ajax/Javascript?

Posted 05 April 2013 - 03:14 PM

Thanks for the reply, I'm sorry I was not clear enough. The PHP code was given to us and I believe we are supposed to add Ajax and Javascript to make it actually work?

This post has been edited by Dormilich: 05 April 2013 - 04:21 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3734
  • View blog
  • Posts: 13,061
  • Joined: 12-December 12

Re: Problem understanding Ajax/Javascript?

Posted 05 April 2013 - 03:21 PM

You mentioned that you've used Javascript before, so you now need to study Ajax.. Perhaps start here at Mozilla.

This may not be the response you were expecting but, until you have studied Ajax and created some code, all we can do is refer you to tutorials and books.

This post has been edited by Dormilich: 05 April 2013 - 04:21 PM

Was This Post Helpful? 1
  • +
  • -

#5 Timeflies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-February 13

Re: Problem understanding Ajax/Javascript?

Posted 05 April 2013 - 03:38 PM

No that is fine! I was just wondering if that is what I needed to add to it. I was just confused on what I actually needed to do for the assignment as not much direction was given(his first semester teaching)

This post has been edited by Dormilich: 05 April 2013 - 04:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3734
  • View blog
  • Posts: 13,061
  • Joined: 12-December 12

Re: Problem understanding Ajax/Javascript?

Posted 05 April 2013 - 04:22 PM

View PostTimeflies, on 05 April 2013 - 10:38 PM, said:

No that is fine! I was just wondering if that is what I needed to add to it. I was just confused on what I actually needed to do for the assignment as not much direction was given(his first semester teaching)

Well you would use Ajax if you wanted, for example, to retrieve some data from a database and insert it into the current page, without having to re-load the page. Or you might want to check whether some information already exists in the database. For example, Ajax can be used while the user tries to key a new username, constantly checking whether the typed username is available, and providing feedback within the current page.

An Ajax request is made, usually sending some data at the same time. A PHP-script processes this request, usually as POST or GET data, and returns information that the current page can intercept. The returned information/data can be in HTML, JSON or XML format.

Good luck!
Was This Post Helpful? 1
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: Problem understanding Ajax/Javascript?

Posted 05 April 2013 - 04:23 PM

note, for PHP there is no differencce between a Request from HTML or AJAX, its both HTTP.
Was This Post Helpful? 0
  • +
  • -

#8 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,906
  • Joined: 30-April 10

Re: Problem understanding Ajax/Javascript?

Posted 05 April 2013 - 04:24 PM

So what I gather you were given all this code and asked to "make it work"?

Here is what I suggest, Take out some paper and write in order what is happening so you can see the flow of the page and then you can see what is missing.

Have you even run the code to see if you get any errors?
Was This Post Helpful? 1
  • +
  • -

#9 Timeflies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 26-February 13

Re: Problem understanding Ajax/Javascript?

Posted 05 April 2013 - 11:38 PM

Yeah basically that's what happened. I ran the loginTester.html and it brought the login.php script up? The PHP was given to us and we are supposed to make it work with Javascript and html I believe. Just not sure where to start! Thanks for all the help though everyone.

This post has been edited by Dormilich: 06 April 2013 - 03:51 PM

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Problem understanding Ajax/Javascript?

Posted 06 April 2013 - 04:17 AM

Quote

we are supposed to make it work with Javascript and html I believe


Pal, you might want to nail down the details of your assignment before you attempt it.

OT: Why would they use the deprecated mysql_* functions in one file, and mysqli in another?

I wouldn't know how to help you. This is your assignment, and YOU know what you've been taught; WE don't. We know how WE would do it, but it likely wouldn't be what the teacher expects.

Here a clue though: Javascript is EVENT-DRIVEN. You write code that responds to events raised by the browser, like a button being pushed or a link being clicked. You use AJAX to send a request to the server and get back (usually) data that you then somehow display to the user, either through an alert box or by adding/changing an HTML element in the current page. When this is done asynchronously -- the typical use case for AJAX -- you create a request object, and create functions which will run based on the result returned from the server. You send your request and when the result comes back the function for the resulting event is called and acts on what's returned.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1