0

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.

7
  • 1
    id attribute must be unique. its not with your case. Commented Apr 28, 2018 at 14:12
  • but id for each record is unique and i am getting it from database i don't understand what you meant Commented Apr 28, 2018 at 14:14
  • id="hidden_user_id" check this. this remains for all record. only value will be changed not id Commented Apr 28, 2018 at 14:16
  • @PatrickEvans yes i think it is the problem can you help me out with the code too? Commented Apr 28, 2018 at 14:17
  • Can you put console.log(hidden_id) after var hidden_id = $('#hidden_user_id').val(); and post the result here? Commented Apr 28, 2018 at 14:17

1 Answer 1

1

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

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Can't make it work it would b great if you elaborate it by using code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.