0

Please guys help me because i can't find out what i can do in order to read my javascript a json file which contains an array with one element.

My php file is working fine and the output is a .json file which contains this line: {"posts":[["30"]]}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
<?php
/* 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'];

$sql= "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"; 
$response = array();
$posts = array();
$result=mysqli_query($link, $sql);
while($row=mysqli_fetch_assoc($result)) { 
  $site_id=$row['site_id'];
  $posts[] = array($site_id);
} 
$response['posts'] = $posts;
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);

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

Can anybody help me what i have to do (without using ajax) in order my javascript function reads that value? I want to rerad this value cause i want to manipulate this number.

    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)

               //i do not want to do with ajax!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!            
                $.ajax({
                   type: 'POST',
                   url: 'http://127.0.0.1/PHP/loo.php',
                   data: $('#LoadGame').serialize(),
                   success: function (html) {
                      //do something on success?
                      $('#outPut').html(html);
                      var bingoValue=4;
                      if( $('#outPut').text().indexOf(''+bingoValue) > 0){
                         //alert('bingo!');
                         window.location.href='https://support.wwf.org.uk/';

               //document.location.replace('https://developer.mozilla.org/en-US/docs/Web/API/Location.reload');
                     }
                     else {
                        alert('No!');
                  }
              }
          });
      }

Thank you for your help!

6
  • on ajax success you are getting a json response isn't? Commented Oct 26, 2018 at 11:26
  • results.json is created on the server. To read it on a page using JS you need ajax. To grab the 30, do $.getJSON('results.json', r => { num = parseInt(r.posts[0][0]); alert(num) }); Commented Oct 26, 2018 at 11:29
  • @ChrisG nice. Where i have to put this line? In my JS function, right? because it doesn't show me an alert and secondly it drives me to my php file.. Commented Oct 26, 2018 at 11:38
  • @DILEEPTHOMAS yes. but can i make with another way the comunication between javascript and php file (which is on the server side) without ajax? Commented Oct 26, 2018 at 11:41
  • @ChrisG i found something. look this:taniarascia.com/how-to-use-json-data-with-php-or-javascript and go to jquery. it says that that can be done without ajax... Commented Oct 26, 2018 at 11:45

1 Answer 1

1

Assuming this PHP code runs during your doc request,

You can read that json if you put it in a script tag

<script type="text/javascript">
    window.myJson = <?php echo(json_encode($response)); ?>
</script

and it will be accessible as window.myJson in frontend

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

6 Comments

thanks for the advice. if you don't mind can you tell me where i hiave to put these lines of code in order to test it?
I have edited the post, you can put it after mysqli_close($link); ?>
unfortunately it says error. Parse error: syntax error, unexpected '<' in C:\wamp64\www\PHP\loo.php on line 33 i believe that the syntax is wrong. how we write script inside php?
I just saw <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> in the start of your code
yes but i think that we sould echo your code with quotes..what do you say?
|

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.