1

I want to move a value from page stepone php to page steptwo php the value is generated automatically by js on stepone php, and then I want to use the same value ( id for the submission ) on steptwo php any ideas ?

<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

  <form action="action_pageone.php" method="POST">
    <label for="FullName">Full Name</label><br>
    <input type="text" id="FullName" name="FullName"><br>


    <!-- this input -->
    <input type="hidden" name="ID" id="ID" value="" maxlength="16" size="16">
    <!-- this input -->


    <input type="submit" value="Submit">
  </form>

  <script>
    function randomNumber(len) {
      var randomNumber;
      var n = '';

      for (var count = 0; count < len; count++) {
        randomNumber = Math.floor(Math.random() * 10);
        n += randomNumber.toString();
      }
      return n;
    }

    window.onload = function() {
      document.getElementById("ID").value = randomNumber(9);
    };
  </script>
</body>

</html>

and the action_pageone here it is so i need to copy this ID to the other page mentioned ( steptwo) the exact ID

<?php

$ID = $_POST['ID'];
$FullName = $_POST['FullName'];

// Database Connection
$servername = "localhost";
$username = "";
$password = "";
$db = "";

$conn = new mysqli('localhost', 'username', 'password', 'db') or die($conn->connect_error);
$conn->set_charset("utf8");

if ($conn->connect_error){
    die("Connection failed: ". $conn->connect_error);
}
$sql = "insert into Registration(ID,FullName) values('$ID','$FullName')";

if ($conn->query($sql) === TRUE) {
    header('location: steptwo.php');
} else {
    echo " Registration not Success ";
}

$conn->close();

?>

any suggestions guys ?

for me it doesnt matter if its js or php , i need it to work :(

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • When you press submit you will go to e`action_pageone.phpe` then you can use simple `$_POST['ID'];`, if you need again you can use another hidden input with value of the post – Simone Rossaini Jul 28 '21 at 09:52
  • ther main problem is when i move to the next page the value reset to 0 – George Kass Jul 28 '21 at 09:57
  • **Warning!** You're open to [SQL injection attacks](https://owasp.org/www-community/attacks/SQL_Injection)! Read [how to prevent SQL injection in PHP](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) by using prepared statements with bound parameters instead of injecting variables directly into your queries. It's not just about security. If your data contains, for example, a single quote `'`, your query will break. – M. Eriksson Jul 28 '21 at 10:04

3 Answers3

0

You can use the Web Cache,like cookie or session,then use the php to read this ID value.

JJBoy
  • 36
  • 3
0

there is many ways to do it

  • use input type hidden and keep pushing it on submit buttons
  • pass it in URL like google.com?id=2&name=mike and fetch it using $_GET['id']
  • keep it in session/cookie/js webstorage
CBroe
  • 91,630
  • 14
  • 92
  • 150
Shubham Dange
  • 173
  • 13
0

You can do this in 2 ways, use hidden form and submit it or use ajax to post it.

hidden form

<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

  <form action="action_pageone.php" method="POST" name="form1">
    <label for="FullName">Full Name</label><br>
    <input type="text" id="FullName" name="FullName"><br>


    <!-- this input -->
    <input type="hidden" name="ID" id="ID" value="" maxlength="16" size="16">
    <!-- this input -->


    <input type="button" onclick = "submit()" value="Submit">
  </form>
<form action="action_pagetwo.php" method="POST" name="form2"> 
    <!-- this input -->
    <input type="hidden" name="ID" id="ID" value="" maxlength="16" size="16">
    <!-- this input -->
  </form>

  <script>
function submit()
{
  document.forms['form1'].submit();
  document.forms['form2'].submit();
  //wait what form to submit?.. confused...? yes me 2 lol
}
   
    function randomNumber(len) {
      var randomNumber;
      var n = '';

      for (var count = 0; count < len; count++) {
        randomNumber = Math.floor(Math.random() * 10);
        n += randomNumber.toString();
      }
      return n;
    }

    window.onload = function() {
      document.getElementById("ID").value = randomNumber(9);
    };
  </script>
</body>

</html>

This might a way to achieve what you want but might be inappropriate as after the form gets submitted it actually redirects to the action page. But still you can sort out how you want your response by yourself.

Ajax (after receiving ID in your first php file you pass it to the second one)

<?php

$ID = $_POST['ID'];
$FullName = $_POST['FullName'];
if($ID != NULL)
echo '<script> 
$.ajax({
url: "action_pagetwo.php",
type: "post",
data: {
  id: '.$ID.'
},
success: function(result) {
  console.log("done bro");
}
});
</script>';

// Database Connection
$servername = "localhost";
$username = "";
$password = "";
$db = "";

$conn = new mysqli('localhost', 'username', 'password', 'db') or die($conn->connect_error);
$conn->set_charset("utf8");

if ($conn->connect_error){
    die("Connection failed: ". $conn->connect_error);
}
$sql = "insert into Registration(ID,FullName) values('$ID','$FullName')";

if ($conn->query($sql) === TRUE) {
    header('location: steptwo.php');
} else {
    echo " Registration not Success ";
}

$conn->close();

?>

You can pass the ID in your html page itself to both php files, but you should choose what you want, you want to pass it without redirecting or you wanna direct to page by submitting.

Mohammed Khurram
  • 616
  • 1
  • 7
  • 14