Nabeel_Afzal
Nabeel_Afzal

Reputation: 155

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

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 ajax I tried to navigate to other page but didn't succeed in that too. How can i pass the data and receive on other page without showing in query string? ajax code is

var id = $(this).data('username');
            $.ajax({


        var id = $(this).data('username');
        $.ajax({type: "POST",
        cache: false,
        data: id,
        url: "Update.php",
        success: function(dto)
        {
            //but I do not require this return call I just
           // want to pass the data to update.php
        }
    });

    //this is the code where the button is being clicked
    <table class="table table-condensed" >

        <thead style="background-color:#665851" align="center">
        `<tr>
            <td style="color:white">Roll No</td>
            <td style="color:white">Name</td>
            <td style="color:white">Department</td>
            <td style="color:white">Click To Update</td>
        </tr>

        </thead>
        <tbody style="background-color:whitesmoke;  border:initial" id="tblBody" align="center">

<?php


                $database="firstdatabase"; //database name
     $con = mysqli_connect("localhost","root" ,"");//for wamp 3rd field is balnk
     if (!$con)
     {     die('Could not connect: ' . mysql_error());
     }

     mysqli_select_db($con,$database );
    $state = "SELECT rollno ,name, dept FROM student ;";

     $result = mysqli_query($con,$state);   
      $output = 1;
      $outputDisplay = "";
      $noRows = mysqli_affected_rows($result);
     if($result)
    {

        $num = mysqli_affected_rows($con);
                //$row = mysqli_fetch_array($result,MYSQLI_NUM);

        while ($row = mysqli_fetch_array($result)) 
        {

        $r = $row['rollno'];
        $n = $row['name'];
        $d = $row['dept'];
        $outputDisplay .= "<tr><td>".$r."</td><td>".$n."</td><td>".$d."</td><td align='right'>
        <button type='button' name='theButton' value='Detail' class='btn' id='theButton' data-username='$r'> <img src='edit.jpg'/></button>
        </td>
        </tr>";
        }
    }
    else
    {
        $outputDisplay .= "<br /><font color =red> MYSql Error No: ".mysqli_errno();
        $outputDisplay .= "<br /> My SQl Error: ".mysqli_error();


    }



    ?>
    <?php 
     print $outputDisplay;
    ?>
        </tbody>
    </table>

Upvotes: 1

Views: 167

Answers (6)

guest271314
guest271314

Reputation: 1

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");

Upvotes: 1

Mathias_
Mathias_

Reputation: 323

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
}

Upvotes: 0

Jos&#233; M. Carnero
Jos&#233; M. Carnero

Reputation: 263

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.

Upvotes: 0

Ram Segev
Ram Segev

Reputation: 2573

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

Upvotes: 0

Marko Krstic
Marko Krstic

Reputation: 1447

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.

Upvotes: 0

Suman
Suman

Reputation: 401

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

Upvotes: 0

Related Questions