0

How can I send the lastInsertId again to another php document?

Explanation why: After someone sends a form (form.html) to a database (send.php) he will get an ID for that form. This ID I show in the send.php to the person via PDO:

<p>Your ID:<?php echo $dbh->lastInsertId(); ?></p>

At this confirmation page I want give the person the possibility to print the data from his form as an pdf. So I wrote:

<form action="print.php" method="POST"> 
<input type="hidden" name="ID" value="<=htmlspecialchars($_POST['lastInsertId']);?>"/>
<input type="submit" name="Print" value="Print" >
</form>

But I he doesn't send the lastInsertId -> I guess the problem is here:

value="<?=htmlspecialchars($_POST['lastInsertId']);?>"

Can you help me to solve that problem?

Jessy642
  • 67
  • 8
  • You can pass the id in the URl. So something like www.example.com?id=343 and so on, its been a while since I have used PHP so can't recall what the actually name for this process is. Have a look here http://html.net/tutorials/php/lesson10.php – SANM2009 Dec 28 '17 at 12:38

3 Answers3

1

Your code should be like this:

<form action="print.php" method="POST"> 
<input type="hidden" name="ID" value="<?php echo $dbh->lastInsertId(); ?>"/>
<input type="submit" name="Print" value="Print" >
 </form>
SANM2009
  • 1,918
  • 2
  • 12
  • 30
0

Not sure if it's the best way, but I once needed the last ID of a person who registered. I did the following after inserting the info into the DB (My table has Auto Increment Primairy Key ID):

$lastId = mysqli_insert_id($con);

You can store the ID anywhere you want. (In the URL or cookie)

Hope this helps (:

JeroenM
  • 807
  • 1
  • 11
  • 26
0

Thank you very much! That helped.

Is their an easy way that after he press print (now gets with the ID all the data from the database to show it at the site print.php - that part all works) and NOW DIRECTLY asks to save as pdf?

Jessy642
  • 67
  • 8
  • There are scripts around that would print a page to PDF. Search Google for this. I wouldn't say that would be easy, but the plugin / script you find will give you details on how to use it. – SANM2009 Dec 28 '17 at 15:06
  • Great I will try that. – Jessy642 Dec 28 '17 at 15:26