javascript - jQuery calls a PHP file to get data from mysql database? -
ok have found number of tutorials online , have followed each of them step step. problem know javascript/jquery better know php , cannot figure out how debug going wrong in section. have bunch of buttons , , when button pressed determines default values in form.
jquery side
$(document).ready(function(){ // cspc , addupdate toggle defined globally $('ul#parts').on('click', 'button', function(){ addupdatetoggle = "add"; cspc = $(this).attr("data-cspc"); var form = $('div.sidebar form'), sr = 0; form.find("#cspc").val(cspc); $.ajax({ type: "get", url: "getrate.php", data: "pid=a5843", datatype: "json", success: function(data){ sr = data; } }); form.find("#strokerate").val(sr); showform(); }); });
php side
<?php $host = "localhost"; $user = "username"; $pass = "password"; $databasename = "movedb"; $tablename = "part parameters"; $con = mysql_connect($host, $user, $pass); $dbs = mysql_select_db($databasename, $con); //get parameter url $pid=$_get["pid"]; if (empty($pid)){ echo "1"; //default rate } else{ $db=mysql_pconnect("localhost");//connect local database mysql_select_db("movedb", $db);//select database want use if (!$db){ echo ("error connecting database"); } else{ //connection successful $sql = "select 'processing rate (ppm)' 'part parameters' 'part number' '" . $pid . "'";//sql string command $result=mysql_query($sql);//execute sql string command //result contains rows $rows = mysql_fetch_row($result) echo json_encode($rows["processing rate (ppm)"]); } } ?>
any ideas why sr not getting set?
am way off base?
i shamelessly note not know $user , $pass should set to. cannot find explained anywhere
thanks in advance!
edit: followed of directions below , when run
http://localhost/getrate.php?pid=a5843
it says "no database selected." also, dont have access our original ms access file (one of team members has it) once make headers 1 word headers. our first job web programming/database management learning.
$user
, $pass
should set mysql user's username , password.
i'd use this:
js
success: function(data){ if(data.status === 1){ sr = data.rows; }else{ // db query failed, use data.message error message } }
php:
<?php $host = "localhost"; $user = "username"; $pass = "password"; $databasename = "movedb"; $tablename = "part parameters"; $con = mysql_pconnect($host, $user, $pass); $dbs = mysql_select_db($databasename, $con); //get parameter url $pid = $_get["pid"]; if(empty($pid)){ echo json_encode(array('status' => 0, 'message' => 'pid invalid.')); } else{ if (!$dbs){ echo json_encode(array('status' => 0, 'message' => 'couldn\'t connect db')); } else{ //connection successful $sql = "select `processing rate (ppm)` `part parameters` `part number` `" . mysqli_real_escape_string($pid) . "`"; //sql string command $result = mysql_query($sql) or die(mysql_error());//execute sql string command if(mysql_num_rows($result) > 0){ $rows = mysql_fetch_row($result); echo json_encode(array('status' => 1, 'rows' => $rows["processing rate (ppm)"]); }else{ echo json_encode(array('status' => 0, 'message' => 'couldn\'t find processing rate give pid.')); } } } ?>
as user said, should try renaming database fields without spaces part parameters
=> part_parameters
, part number
=> part_number
.
if you're still having trouble (as long it's not production server) put @ top of php file:
error_reporting(e_all); ini_set('display_errors', '1');
this output errors , should work out what's going wrong.
Comments
Post a Comment