Passing data between PHP and JavaScript? -
the overall idea drawing graph of somebody's scores in div. i've got button when clicked runs graph drawing function. have function retrieves data database using switch statement function shared other buttons.
my data retrieval function:
var getdata = function(button_id) { $.ajax({ type: "post", url: "../scripts/getdata.php", datatype: "html", data: {id: button_id}, success: function(result){ $("#profilebox").html(result); } }); };
runs getdata.php , returns values blank div.
getdata.php:
<?php session_start(); $switchcase = $_post['id']; $email = $_session['user']['email']; //connect database here $result=mysqli_query($con,"select * users email = '$email'"); switch ($switchcase) { case "profile_home": while($row = mysqli_fetch_array($result)) { echo $row['username'] . "'s profile<br><br>"; echo "name: " . $row['firstname'] . ' ' . $row['lastname'] . "<br><br>"; echo "things like:<br>"; echo $row['like'] . "<br><br>"; echo "things dislike:<br>"; echo $row['dislike'] . "<br><br>"; echo "other sports:<br>"; echo $row['sports']; }; break; case "profile_scores": while($row = mysqli_fetch_array($result)) { $row['correct']; $row['incorrect']; }; break; case "profile_merits": //code goes here; break; case "profile_help": //code goes here; break; case "profile_edit": //code goes here; break; } mysqli_close($con); ?>
receives posted div id (profile_scores), gets data database, switches second case. here problem is, i'm not sure how pass the
$row['correct']; $row['incorrect'];
values original page , them show in graph where
/* correct value */
and
/* incorrect value */
are.
graph drawing function:
function drawvisualization() { getdata("profile_scores"); // create , populate data table. var data = google.visualization.arraytodatatable([ ['', 'correct', 'incorrect'], ['scores', /* correct value */, /* incorrect value */], ]); var options = { 'title': 'total scores overall', 'width': 600, 'height': 400, 'haxis': {title: ''}, 'backgroundcolor': 'transparent' }; // create , draw visualization. new google.visualization.columnchart(document.getelementbyid('profilebox')). draw(data, options); };
this function ran when user clicks button calls getdata function , draws graphusing values received getdata.php.
any appreciated :)
i totally not sure asking here guess.
if want $result fetched in case 2 database on page. can use json pass php array javascript , rest of job. suppose case no 2 of switch statement
case "profile_scores": { $row = $result->fetch_array(mysqli_num); echo json_encode($row); break; }
and in ajax response this
$.ajax({ type: "post", url: "../scripts/getdata.php", datatype: "html", data: {id: button_id}, success: function(result){ console.log(result); console.log(json.parse(result)); var phparray = json.parse(result); var correct = phparray[12]; //<---this return 100 var incorrect = phparray[13]; //<---this return 10 //i dont know 1 correct or incorrect column number can have idea } });
this way got php array javascript database result. suggest correct switch syntax. won't make difference should taken care correct syntax.
switch($switchcase) { case "case1": { //code go break; } case "case2": { //code go break; } case "case3": { //code go break; } }
note have put break; command inside switch case
Comments
Post a Comment