Zohaib
Zohaib

Reputation: 159

Passing Hidden ID value to another page using javascript php

I am trying to pass hidden value from page to another page and it work fine only for first record however for other records it's showing error Here is the code:

$sql = "SELECT id,jdes,title FROM job";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    ?>
    <input type="hidden" id="hidden_user_id" value="<?php echo  $row["id"]  ?>">
    <h3><?php echo  $row["title"]  ?>:</h3>
    <p class="lead">
      <?php echo  $row["jdes"]  ?> 
    </p>
    <button type="button" id="requestthis" class="btn btn-primary">
      <a href="jobs-inner.php">Request</a>
    </button>
    <?php

  }
} else {
  echo "Nothing to display Yet";
}
?>

jobs-inner.php

<?php  

echo $_GET['hidden_id'];

?>

Javascript:-

$(function() { //ready function
    $('#requestthis').on('click', function(e){ //click event
        e.preventDefault(); 
        var hidden_id = $('#hidden_user_id').val();
        var url = "jobs-inner.php?hidden_id="+hidden_id;

        window.location.replace(url);
    })
})

Error:-

 Undefined index: hidden_id in C:\wamp64\www\project\jobs-inner.php on line 3

It might be a simple problem but I am a beginner and I can't figure it out.

Upvotes: 0

Views: 460

Answers (1)

Sarpyjr
Sarpyjr

Reputation: 177

Your value is unique but the id isn't. Make the id of the input unique something like below.

<input type="hidden" id="hidden_user_<?php echo  $row["id"]  ?>" value="<?php echo  $row["id"]  ?>">

but you would have to do a count on code below to make it display base on how many rows you have.

<?php  
echo $_GET['hidden_id'];
?>

Without JavaScript

$sql = "SELECT id,jdes,title FROM job";
$result = $conn->query($sql);
$count = 1;
    if ($result->num_rows > 0) {
    // output data of each row
        while($row = $result->fetch_assoc()) {
        ?>

        <input type="hidden" id="hidden_user_<?php echo $count ?>" value="<?php echo $row["id"] ?>">
        <h3><?php echo  $row["title"]  ?>:</h3>
        <p class="lead"><?php echo  $row["jdes"]  ?></p>
        <form id="<?php echo $count ?>" action="jobs-inner.php?hidden_id=<?php echo $row["id"] ?>" method="post">
        <input type="submit" vaule="Request">
        </form>                     
        <?php
        $count++;  
        }
    } else {
        echo "Nothing to display Yet";
    }

?>

Upvotes: 1

Related Questions