Jump to content

pass the id from one page to another


maideen
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi
I am new, Pls help me the following issue. When click the update button, ID should be to another page to process the update function. Below is my entire code

index.php

<?php
include_once '../../inc/config.inc.php';

$sql="SELECT * FROM demo  order by id";
    try {
            $result = mysqli_query($con,$sql);  
            include 'view.html.php'; 
         } 
    catch (PDOException $e) 
        {
           echo $e->getMessage() . "\n";
           file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
//           $output = 'Error fetching authors from database!';
//           include '../../notification/errormsg.php';
           exit();
        }

view.html.php

<Script Language="javascript">
function change_action()
    {
        var frm_obj=document.getElementById("frm");
        frm_obj.action="data.php";
    }
</Script>

<h3>User Details</h3>
<form action="" method="POST" id="frm" >
    <table width="100%" align="center" cellpadding="4" cellspacing="1">
        <tr>
            
            <td>ID</td>
            <td>ID</td>
            <td>NAME</td>
            <td>FIRST NAME</td>
            <td>AGE</td>
            <td></td>
            
        </tr>
        <?php
            if(isset($result)){
            while($row = mysqli_fetch_array($result)){ ?>
             <tr>
                
                <td><input type="text" name="vid" value="<?php echo $row['id'];?>"/></td>
                <td><?php echo $row['id'];?></td>
                <td><?php echo $row['name'];?></td>
                <td><?php echo $row['firstname'];?></td>
                <td><?php echo $row['age'] ;?></td>
                <td><input type="submit" value="update" name="update" onclick="change_action()">
                <input type="submit" value="delete" name="delete" onclick="change_action()">
                </td>                
              </tr>  
         <?php
            } 
         
            }
            mysqli_close($con);
        ?>
   
    </table>
</FORM>
<?php
include '../../inc/footer.php';

data.php

<Script Language="javascript">
function change_action()
    {
        var frm_obj=document.getElementById("frm");
        frm_obj.action="data.php";
    }
</Script>

<h3>User Details</h3>
<form action="" method="POST" id="frm" >
    <table width="100%" align="center" cellpadding="4" cellspacing="1">
        <tr>
            
            <td>ID</td>
            <td>ID</td>
            <td>NAME</td>
            <td>FIRST NAME</td>
            <td>AGE</td>
            <td></td>
            
        </tr>
        <?php
            if(isset($result)){
            while($row = mysqli_fetch_array($result)){ ?>
             <tr>
                
                <td><input type="text" name="vid" value="<?php echo $row['id'];?>"/></td>
                <td><?php echo $row['id'];?></td>
                <td><?php echo $row['name'];?></td>
                <td><?php echo $row['firstname'];?></td>
                <td><?php echo $row['age'] ;?></td>
                <td><input type="submit" value="update" name="update" onclick="change_action()">
                <input type="submit" value="delete" name="delete" onclick="change_action()">
                </td>                
              </tr>  
         <?php
            } 
         
            }
            mysqli_close($con);
        ?>
   
    </table>
</FORM>
<?php
include '../../inc/footer.php';

edit.php

<?php
echo 'edit.php';
  
if(isset($result))
    {
        while($row = mysqli_fetch_array($result))
                { 
                    echo $row['id'];'<br>';
                    echo $row['name'];'<br>';
                    echo $row['firstname'];'<br>';
                    echo $row['age'];'<br>';
    
                }
    }

Tq

 

Maideen

data.php

edit.php

index.php

view.html.php

Link to comment
Share on other sites

Hi Guru

 

This is the post action

 

Basically page shows all data as table format with id. If i click the selected update button, the id of selected row to be posted into next pages to retrive the data of the particular Id. Can you pls explain how to do?

 

<form action="" method="POST" id="frm" >
<table width="100%" align="center" cellpadding="4" cellspacing="1">
<tr>

<td>ID</td>
<td>ID</td>
<td>NAME</td>
<td>FIRST NAME</td>
<td>AGE</td>
<td></td>

</tr>
<?php
if(isset($result)){
while($row = mysqli_fetch_array($result)){ ?>
<tr>

<td><input type="text" name="vid" value="<?php echo $row['id'];?>"/></td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['firstname'];?></td>
<td><?php echo $row['age'] ;?></td>
<td><input type="submit" value="update" name="update" onclick="change_action()">
<input type="submit" value="delete" name="delete" onclick="change_action()">
</td>
</tr>
<?php
}

Link to comment
Share on other sites

It looks like you have one form which contains many input fields named "vid". Unless I'm missing something, that's not going to work. When the form is submitted, only the last value will be passed through the form.

 

Instead, I would recommend looking into using anchor tags for the action buttons. For example, you could try something like the following:

<?php
if(isset($result)){
     while($row = mysqli_fetch_array($result)){
          echo '<tr>';
          echo "<td>{$row['id']}</td>";
          echo "<td>{$row['name']}</td>";
          echo "<td>{$row['firstname']}</td>";
          echo "<td>{$row['age']}</td>";
          echo "<td><a href='data.php?update=1&vid={$row['id']}'>Update</a> <a href='data.php?delete=1&vid={$row['id']}'>Delete</a></td>";
          echo '</tr>';
     }
}
?>
 
Then on the data.php page, you could see which link was clicked by doing something like this:
<?php
if(isset($_GET['update'])) {
     //do update stuff
} elseif(isset($_GET['delete'])) {
     //do delete stuff
}
?>
Edited by cyberRobot
Link to comment
Share on other sites

I am telling the scnario. I have page with all customer data as grid format. last column is "UPDATE". If I select 5th row and click update, next page open the details of 5th row customer data.

My sql command:

 

$sql="select id,name,firstname,age from customer where id='$id'"

 

How can i pass the id for select customer to the next page to retrive data from mysql database and to update if any changes.

 

Pls help me

 

ID      NAME           FIRST NAME   AGE

1       Hoffman      Tatyana           100   Update

2      Atkins            Ishmael             44   Update

3      Hamilton        Mohammad     73   Update

4      Murray          Troy                  18   Update

5      Schwartz       Carla                 79   Update

6      Bond             Leno                 33   Update

7     Noble             Georgia            88   Update

8     Frederick       Bradley             80   Update

9     Chambers      Aphrodite         63   Update

10   Schultz          Allegra             90   Update

11    Garrett         Lillith                20   Update

12    Mcdaniel      Venus              89   Update

13    Acevedo      Iola                 26   Update

Link to comment
Share on other sites

if you are setting up the UPDATE link as cyberRobot suggested then you can retrieve the users id using $_GET['vid']

if(isset($_GET['update']))
{
   $id = intval($_GET['vid']); // get the user id, sanitize it before using it an SQL query
   $sql="select name, firstname, age from customer where id=$id"; // select name, firstname and age fields, no need to get id field again as we already have it ($id)
   ...
}
Link to comment
Share on other sites

hi Ch0cu3r

 

When I use below code, i got id of last record. If I have 100 record in database, I shows last record i.e 100. But I need what I selected. Pls help me 

<Script Language="javascript">
function change_action()
    {
        var frm_obj=document.getElementById("frm");
        frm_obj.action="data.php";
    }
</Script>
<form action="" method="POST" id="frm" >
    <table width="100%" align="center" cellpadding="4" cellspacing="1">
        <tr>
            
            <td>ID</td>
            <td>ID</td>
            <td>NAME</td>
            <td>FIRST NAME</td>
            <td>AGE</td>
            <td></td>
            
        </tr>
     
     <?php
            if(isset($result)){
            while($row = mysqli_fetch_array($result)){ ?>
             <tr>
                
                <td><input type="hidden" name="vid" id="id" value="<?php echo $row['id'];?>"></td>
                <td><?php echo $row['id'];?></td>
                <td><?php echo $row['name'];?></td>
                <td><?php echo $row['firstname'];?></td>
                <td><?php echo $row['age'] ;?></td>
                <td><input type="submit" value="update" name="update" onclick="change_action()">
                <input type="submit" value="delete" name="delete" onclick="change_action()">
                </td>                
               
             </tr>  
         <?php
            } 
         
            }
            mysqli_close($con);
        ?>
<?php
include_once '../../inc/config.inc.php';
    if (isset($_POST['update']) && $_POST['update']  != "" )
        {
            $id = $_POST["vid"];
            $sql="SELECT * FROM demo where id='$id'";
            $result = mysqli_query($con,$sql);
            include 'edit.php';  
            //header("Location:edit.php"); 
     }
Edited by maideen
Link to comment
Share on other sites

  • Solution

Re-read cyberRobots reply (import bits are in bold)

It looks like you have one form which contains many input fields named "vid". Unless I'm missing something, that's not going to work. When the form is submitted, only the last value will be passed through the form.

Instead, I would recommend looking into using anchor tags for the action buttons.

 

Either do as cyberRobot suggested. Or change your code so it outputs a new form for each row.

 

 

.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.