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?

6
  • 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. Commented Oct 25, 2018 at 8: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); Commented Oct 25, 2018 at 8: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.. Commented Oct 25, 2018 at 9:00
  • @JohnnyCage ok...i prepare an example for you and hope this is helpful Commented Oct 25, 2018 at 9:15
  • @Ferdinando thank you very much for your help..I am looking foward your help. :) Commented Oct 25, 2018 at 9:19

1 Answer 1

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...

Sign up to request clarification or add additional context in comments.

6 Comments

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??
did you modified the row in php file? file_put_contents('data.json', "data ="."'"."[".$myJSON."]';");
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
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
do not apologize..thank you for your help and for your patience my friend. no it doesn't show anything now..any other ideas?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.