-2

I would like to try to find a way to reload my HTML page without making it refresh. For example, I want through a PHP file to reload my 1st page to a second page. Here is my try, but it shows me this message --> Parse error: syntax error, unexpected '<' in C:\wamp64\www\PHP\pesto.php on line 26

Here is my php file:

  <?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'];

    //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'];
    }
    if ($res){
        if($result[0] <20){
           <script>
  $.get("updated-content.html", function(data, status){ $("body").html(data); }).fail(function(e) { alert( "error" +JSON.stringify(e)); })
  </script>
            });
        }
    }

    // while($row = mysqli_fetch_array($res)){
    //     array_push($result, array($row[0]));}
    // echo json_encode(array($result));

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

I don't want to use PHP redirection. I want to make that happen with the usage of jquery. Is it possible?

JohnnyCage
  • 29
  • 1
  • 7
  • Can you please point out which is line 26? – Thum Choon Tat Oct 05 '18 at 06:18
  • 1
    Welcome. `$.get("updated-content.html",` Is that Javascript inside your PHP? – brombeer Oct 05 '18 at 06:20
  • Yes, of course @ThumChoonTat. It's this part: `` – JohnnyCage Oct 05 '18 at 06:20
  • 1
    You've to echo your jquery, not mess it with PHP. – AnTrakS Oct 05 '18 at 06:20
  • @kerbholz yes. it's javascript. Because i don't want to use php redirect..Do you have any other ideas? – JohnnyCage Oct 05 '18 at 06:21
  • @D.Dimitrov can you show me what command i have to use? Because maybe i don't know how to do it.. – JohnnyCage Oct 05 '18 at 06:21
  • You can use `header()` to redirect to a different page. Or if you want to use ` – brombeer Oct 05 '18 at 06:22
  • `echo ' });';` – AnTrakS Oct 05 '18 at 06:22
  • Take a look at the "Ajax" concept. – paskl Oct 05 '18 at 06:22
  • "_I don't want to use PHP redirection._" Why is that? Bad experiences with PHP redirection? What if a user has Javascript disabled? – brombeer Oct 05 '18 at 06:23
  • You have right about header. But i want to do with the script. Ok, im gonna try right now and i will tell you if i have any problems.. – JohnnyCage Oct 05 '18 at 06:23
  • Check my answer! You can change the page content on the same URL (With page refresh) but without redirects and JS! – PlanetCloud Oct 05 '18 at 06:29
  • @JasonNathanael i think we are very close to solve this issue. Now it shows me a blank page with the url `http://127.0.0.1/PHP/pesto.php` – JohnnyCage Oct 05 '18 at 06:32
  • @D.Dimitrov i did it but it doesn't show me the updated-content.html page. Should i have to use dots or backslashes in front of my file name? – JohnnyCage Oct 05 '18 at 06:34
  • WOW, EVEN stackoverflow recognizes syntax error, @ echo ; - opening and closing script tag has different color. Impressive isn't it – BILAL MALIK Oct 05 '18 at 06:47
  • @BILALMALIK yes you have right...haha it's the best.. – JohnnyCage Oct 05 '18 at 06:52
  • @JohnnyCage - Yes Indeed :) – BILAL MALIK Oct 05 '18 at 06:59

1 Answers1

2

The error unexpected syntax clearly explains what the problem is... it does not expect that to be there!
Common fix : Check your semicolon and brackets. It is obious you MUST use a " or '.

if ($res){
    if($result[0] <20){
    echo '<script>
            $.get("updated-content.html", function(data, status){ $("body").html(data); }).fail(function(e) { alert( "error" +JSON.stringify(e)); })
          </script>';
    }
}

Also you should be using prepared statements as your code above is vulnerable to SQL injection. You can learn more by Googling "SQL Injection" and "Prepared Statements PHP".

You can also use PHP to include or require files based on conditions...
Such as :

<?php

if ($res){
    if($result[0] <20){
        include 'updated-content.html';
    }else{
        include 'file.html';
    }
}

?>
PlanetCloud
  • 314
  • 2
  • 11
  • 2
    You have syntax error, too. – AnTrakS Oct 05 '18 at 06:24
  • Whoops, sorry for that... fixed. – PlanetCloud Oct 05 '18 at 06:26
  • 1
    @JasonNathanael thank you for your help. Now it doesn;'t show me errors but it doesn;t show me also my updated-content.html page. I have placed this page inside www folder in my wamp directory. Do you know if i have to use some slashes or dots in front of file name? – JohnnyCage Oct 05 '18 at 06:31
  • @JohnnyCage, hit `F12` on your keyboard and it should open a 'Developer Console'. Go to the `Console` section and look for errors such as `404 Page Not Founded`. – PlanetCloud Oct 05 '18 at 06:33
  • 1
    @JasonNathanael this is cool. Thank you! It shows me this: `Uncaught ReferenceError: $ is not defined at pesto.php:2` – JohnnyCage Oct 05 '18 at 06:35
  • Okay, is the `$` you wanted is jQuery? is yes then include the jQuery library BEFORE the code above. – PlanetCloud Oct 05 '18 at 06:36
  • @JasonNathanael i can't believe it!!! It works finally. Thank you so much! Do you want to post your answer in order to give you thumbs up in stackoverflow? – JohnnyCage Oct 05 '18 at 06:41
  • @JohnnyCage I've posted my answer and you're currently commenting on it. Accept the answer if this solves your problem. Thank you and happy coding :D – PlanetCloud Oct 05 '18 at 06:42
  • I wish you have anice day. Thank you so much for your helpa again! I was stacked yesterday and i couldn't find a solution to this.. – JohnnyCage Oct 05 '18 at 06:43