how to pass data from one html page to second in php?

后端 未结 6 1591
南笙
南笙 2021-01-23 15:05

I have created a page for updation of record. I want to pass the id of student from one page to another. I am trying to send it through window.location but it is not working. In

相关标签:
6条回答
  • 2021-01-23 15:16

    you can try using cookies

    Set the cookie
        <?php
        setcookie("name","value",time()+$int);
        /*name is your cookie's name
        value is cookie's value
        $int is time of cookie expires*/
        ?> 
    Get the coockie
    
        <?php
        echo $_COOKIE["your cookie name"];
        ?>
    
    0 讨论(0)
  • Populate a <form method="post" [...]> in the first page with the information needed; you can change their aspect with CSS as desired.

    When the <form> is send you only need a PHP script/page that uses $_POST to fill the new page.

    Easier than AJAX if you try to navigate from the first page to the second.

    0 讨论(0)
  • 2021-01-23 15:25

    If you already have a form and wanna post that to an update script, you could just add the student id as an hidden form element example:

    <input type="hidden" name="student_id" value="<?php echo $student_id; ?>">
    

    Else if you want to redirect from another page to the update page, with a student id, the best way will probably be a $_GET variable. So the URL would look something like this: http://domain.com/update.php?student_id=1

    And then your update.php will include a simple check like this.

    if(!empty($_GET['student_id'])) {
        $student_id = $_GET['student_id'];
        // Ready to update
    } else {
        // Throw 404 error, or redirect to an create page
    }
    
    0 讨论(0)
  • 2021-01-23 15:34

    When you use window.location then your page go to another page. ajax work on active page. You can not use.

    0 讨论(0)
  • 2021-01-23 15:41

    If both pages are at same domain you can use localStorage, storage event to pass data between html documents

    At second page

    window.addEventListener("storage", function(e) {
      // do stuff at `storage` event
      var id = localStorage.getItem("id");
    });
    

    at first page

    // do stuff, set `localStorage.id`
    localStorage.setItem("id", "abc");
    
    0 讨论(0)
  • 2021-01-23 15:42

    Generally you can use sessions for this $_SESSION variable to store it into the session, or you can pass that value via get parameter. And afterwards get that parameter with $_GET

    Or $_POST parameter if you want to submit form.

    0 讨论(0)
提交回复
热议问题