Home > database >  Why is my php page showing an error on the <?php tags
Why is my php page showing an error on the <?php tags

Time:09-27

I am trying to create a webpage which only uses PHP (there does not need to be any HTML elements) but when I run the page I get the following error: enter image description here

Here is the code for my page:

<script>
<?php
    if(isset($_COOKIE['string']) and isset($_COOKIE['studentarrays'])){
        $string = $_COOKIE['string'];
        $array = explode(",",$string);
        array_shift($array);
        $studentstring = $_COOKIE['studentarrays'];
        $studentarrays = explode(",",$studentstring);

?>
    var array = <?php echo $studentarrays ?>;
    alert(array);
<?php
    }
?>
</script>

enter image description here

What have I done wrong?

Edit: As someone pointed out I had a SESSION variable without starting the session, so this part of the error has now been fixed. Also I had forgotten to include the session code when I copied my code to this website, sorry for the confusion. Thank you for all the replies, it seems that I have made a fair few mistakes with this page so I have decided to go back and simplify this page as much as possible, removing anything which I did not need. Here is my new code:

<script>
<?php
    include"databaseconnection.php";
    session_start();

    if(isset($_COOKIE['studentarrays'])){
        $studentstring = $_COOKIE['studentarrays'];
        $studentarrays = explode(",",$studentstring);
        $teacherid = $_SESSION['username'];
    }
?>
</script>

This seems to be working without errors now but I do not know exactly what it was causing the error. I will update this post if I figure out what part if the original code was causing the error, until then please ignore this question

CodePudding user response:

If you want to passing arrays(php) to JS using JavaScript Object Notation(JSON).

Using json_encode() function: The function can take both single dimensional and multidimensional arrays.

const array = JSON.Parse("<?php echo json_encode($studentarrays) ?>");
    alert(array);

OR

const array = "<?php echo json_encode($studentarrays) ?>";
    console.log(array);

And also you can use PHP implode() function: implode() function is used to build a string that becomes an array literal in JavaScript.

<?php
  
// Creating a PHP Array
$studentarrays = array('John', 'Foo', 'Bar');
  
?>
const array = "<?php echo '["' . implode('", "', $studentarrays) . '"]' ?>";
    console.log(array);

CodePudding user response:

You can't echo on array.

If you want to assign PHP array to json array directly, it's not possible I guess. You could try using JSON, like this:

<script>
<?php
    if(isset($_COOKIE['string']) and isset($_COOKIE['studentarrays'])){
        $string = $_COOKIE['string'];
        $array = explode(",",$string);
        array_shift($array);
        $studentstring = $_COOKIE['studentarrays'];
        $studentarrays = explode(",",$studentstring);

?>
    var array = JSON.parse("<?php echo json_encode($studentarrays) ?>");
    alert(array);
<?php
    }
?>
</script>

Also, the error tells you that your $_SESSION is not defined, have you started session in this file/request cycle?

CodePudding user response:

Try check array contents using empty() instead of isset()

  • Related