Wednesday, 15 January 2014

php - Why am I getting an Ajax error after running this code? -



php - Why am I getting an Ajax error after running this code? -

i getting ajax error. buy.php page user can navigate drop downwards menu select product. 1 time select (via ajax) contents database loaded:

<?php session_start(); $name = $_post['name']; $pass = $_post['password']; //step 1 connect database $host= "localhost"; $dbname= "register"; $user = "root"; $pass = ""; seek { # mysql pdo_mysql $dbh = new pdo("mysql:host=$host;dbname=$dbname", $user, $pass); //$dbh->setattribute( pdo::attr_errmode, pdo::errmode_exception ); $sth = $dbh->query("select username, password tbl_users"); $sth->execute(); //step 2 declare variables $query = $dbh->query("select * tbl_users username='$name' , password='$pass'"); $query->execute(); $query->setfetchmode(pdo::fetch_num); $numrows = $query->fetch(); $_session['name'] = $name; $_session['password'] = $pass; //step 3 check see if user entered of info if(empty($_session['name']) || empty($_session['password'])) { die("could not connect"); } if($name && $pass == "") { die("please come in name , password!"); } if($name == "") { die("please come in name!" . "</br>"); } if($pass == "") { die("please come in password!"); echo "</br>"; } //step 4 check username , password mysql database if($numrows != 0) { $sth->setfetchmode(pdo::fetch_assoc); while($row = $sth->fetch()) { $dname = $row['username']; $dpass = $row['password']; } } else { if( $_session['name']!= $dname || $_session['password'] != $dpass) { header("location: login.php"); } } if($name == $dname && $pass == $dpass) { // if user makes here means logged in echo "hello " . $name . "!"; } } catch(pdoexception $e) { echo "i'm sorry, database connection wrong."; $e->getmessage(); } ?> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script> <script> $(document).ready(function(){ $('#pro').bind('change', function (e){ var value = $(this).val(); e.preventdefault(e) $.ajax({ type: 'get', url: 'product.php', data: value, datatype: 'html', success: function( msg ){ $('#products').html(msg); }); }); }); </script> </head> <body> <p> <select id="pro"> <?php $sth = $dbh->query("select productid, productname, productimage, productprice tbl_products"); $sth->setfetchmode(pdo::fetch_assoc); while($prow = $sth->fetch()) { $pid = $prow['productid']; $pname = $prow['productname']; $pimage = $prow['productimage']; $pimage = $prow['productprice']; echo "<option value='$pid'>".$pname. "</option>"; } ?> </select> </p> <div id="products"></div> </body> </html>

this product page query string passed via ajax , loads contents in buy.php page:

<?php session_start(); $name = $_post['name']; $pass = $_post['password']; //step 1 connect database $host= "localhost"; $dbname= "register"; $user = "root"; $pass = ""; seek { # mysql pdo_mysql $dbh = new pdo("mysql:host=$host;dbname=$dbname", $user, $pass); //$dbh->setattribute( pdo::attr_errmode, pdo::errmode_exception ); $sth = $dbh->query("select username, password tbl_users"); $sth->execute(); //step 2 declare variables $query = $dbh->query("select * tbl_users username='$name' , password='$pass'"); $query->execute(); $query->setfetchmode(pdo::fetch_num); $numrows = $query->fetch(); $_session['name'] = $name; $_session['password'] = $pass; //step 3 check see if user entered of info if(empty($_session['name']) || empty($_session['password'])) { die("could not connect"); } if($name && $pass == "") { die("please come in name , password!"); } if($name == "") { die("please come in name!" . "</br>"); } if($pass == "") { die("please come in password!"); echo "</br>"; } //step 4 check username , password mysql database if($numrows != 0) { $sth->setfetchmode(pdo::fetch_assoc); while($row = $sth->fetch()) { $dname = $row['username']; $dpass = $row['password']; } } else { if( $_session['name']!= $dname || $_session['password'] != $dpass) { header("location: login.php"); } } if($name == $dname && $pass == $dpass) { // if user makes here means logged in echo "hello " . $name . "!"; } } catch(pdoexception $e) { echo "i'm sorry, database connection wrong."; $e->getmessage(); } ?> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script> <script> $(document).ready(function(){ $('#pro').bind('change', function (e){ var value = $(this).val(); e.preventdefault(e) $.ajax({ type: 'get', url: 'product.php', data: value, datatype: 'html', success: function( msg ){ $('#products').html(msg); }); }); }); </script> </head> <body> <p> <select id="pro"> <?php $sth = $dbh->query("select productid, productname, productimage, productprice tbl_products"); $sth->setfetchmode(pdo::fetch_assoc); while($prow = $sth->fetch()) { $pid = $prow['productid']; $pname = $prow['productname']; $pimage = $prow['productimage']; $pimage = $prow['productprice']; echo "<option value='$pid'>".$pname. "</option>"; } ?> </select> </p> <div id="products"></div> </body> </html>

the property list of ajax list missing bracket. success function not closed causing javascript break.

notice } in snippet below:

$.ajax({ type: 'get', url: 'product.php', data: value, datatype: 'html', success: function( msg ) { $('#products').html(msg); } });

php zend-framework jquery cakephp-1.3

No comments:

Post a Comment