0

I want a little help from you guys because I can't figure out what's going on on my javascript code.

I have these lines of code in my php file: (I am sure that my PHP file is working correctly)

PHP file:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<?php
header('Access-Control-Allow-Origin: *');
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("127.0.0.1", "root", "", "mysql3");
// Check connection
if($link === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$user_id =$_POST['user_id'];
$book_id =$_POST['book_id'];
$game_id =$_POST['game_id'];
$site_id =$_POST['site_id'];

//Attempt insert query execution
$query = "SELECT site_id FROM components WHERE user_id='$user_id' && book_id='$book_id' && game_id='$game_id' ORDER BY site_id DESC LIMIT 1";
$res = mysqli_query($link,$query);
$result = array();
$res = mysqli_query($link,$query) or die(mysqli_error($link));
while($row = mysqli_fetch_assoc($res)){
 $result=$row['site_id'];
 $myJSON = json_encode($result);
}
file_put_contents('data.json', $myJSON);//save my data json in file json
echo $myJSON;
//$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
//fwrite($myfile, $result);
//fclose($myfile);

// Close connection
mysqli_close($link);
?>

script:

<script>
    function load3() {
      var flag1 = true;
      do{
        var selection = window.prompt("Give the User Id:", "Type a number!");
            if ( /^[0-9]+$/.test(selection)) {
            flag1=false;
            }
        }while(flag1!=false);
    $("#user_id").val(selection)

      var flag2 = true;
      do{
        var selection2 = window.prompt("Give the Book Id:", "Type a number!");
        if ( /^[0-9]+$/.test(selection2)) {
            flag2=false;
            }
        }while(flag2!=false);
    $("#book_id").val(selection2)

      var flag3= true;
      do{
        var selection3 = window.prompt("Give the Game Id:", "Type a number!");
        if ( /^[0-9]+$/.test(selection3)) {
            flag3=false;
            }
        }while(flag3!=false);
    $("#game_id").val(selection3)

         var mydata =JSON.parse(data);
  alert(mydata[0].name);//get name's value 
}
  </script>

Unofrtunately, i see that the value "4" can't be returned to my js function in my script. What am i doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
JohnnyCage
  • 29
  • 1
  • 7
  • 1
    Why are you embedding jQuery in a server-side script that is supposed to return JSON data? That makes no sense to begin with. – misorude Oct 25 '18 at 08:48
  • If i understood your question you need a file .json example myfile.json with php you put the result in file : file_put_contents('myfile.json', $result); With javascript you add in the script src="myfile.json"... and call it var mydata = JSON.parse(data); – Ferdinando Oct 25 '18 at 08:49
  • @Ferdinando can you show me a tutorial on this? I want the value `site_id` to be returned to my javascript function without ajax...I don't know i i helped you with this.. – JohnnyCage Oct 25 '18 at 09:00
  • @JohnnyCage ok...i prepare an example for you and hope this is helpful – Ferdinando Oct 25 '18 at 09:15
  • @Ferdinando thank you very much for your help..I am looking foward your help. :) – JohnnyCage Oct 25 '18 at 09:19
  • Please read up on [SQL Injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). In your case, it is enough to use `intval` on each id to make sure it is a number. – Adder Oct 25 '18 at 10:45

1 Answers1

2

create manually your file .json(empty) in my example is data.json

your html file:

<!DOCTYPE html>
<html>
<head>
//call your file data.json
<script type="text/javascript" src="data.json"></script>

<script type="text/javascript">
  //use your file data.json
  var mydata =JSON.parse(data);
  alert(mydata[0].name);//get name's value 
</script>
</head>
<body>
</body>
</html>

php file:

<?php

//example data
$myArr = [];
$myArr['name'] = "John";
$myArr['age'] = 30;
$myArr['city'] = "New York";
/*
or object
$myArr = stdClass();
$myArr->name = "John";
$myArr->age = 30;
$myArr->city = "New York";
*/


$myJSON = json_encode($myArr);

file_put_contents('data.json', "data ="."'"."[".$myJSON."]';");//save my data json in file json
echo $myJSON;
?>

php put the data into file data.json in this way: data ='[{"name":"John","age":30,"city":"New York0"}]';

In this situation you can use the file html wrote before. When you will reuse the php file it rewrite the file json. I hope this is helpful...

Ferdinando
  • 964
  • 1
  • 12
  • 23
  • thank you for your help. But i have a small problem.. Inside my file, there is a number with this format "4". I don't care about it. I see that the alert is not working, i edited my code can you take a look please?? – JohnnyCage Oct 25 '18 at 11:35
  • did you modified the row in php file? file_put_contents('data.json', "data ="."'"."[".$myJSON."]';"); – Ferdinando Oct 25 '18 at 12:18
  • of course. because i want the value of `result` to be written in the file. For example the 4 because `site_id` is the 4 number – JohnnyCage Oct 25 '18 at 12:24
  • ok...sorry if i ask somethings... try to put alert("test"); in final row and comment //alert(mydata[0].name)..control if alert is displayed – Ferdinando Oct 25 '18 at 12:34
  • do not apologize..thank you for your help and for your patience my friend. no it doesn't show anything now..any other ideas? – JohnnyCage Oct 25 '18 at 12:49
  • I think that the problem is one of the "do while" that create an infinite loop...try to use one "do while" at a time and control if the alert is displayed – Ferdinando Oct 25 '18 at 12:57