0

Please excuse the poor title. I am a total beginner and don't know the right terms to make it better.

I am trying to POST form data using PHP. My problem is that before i POST the form data i need to get a value from the form, that is changing each time i request the page.
Please notice the second input, the value is auto generated and is a random number each time i request the form.
Here is my form.php:

<?php 

if(!isset($_REQUEST['submit_btn'])){
echo '  
<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
   <input type="text" name="user_name" id="user_name">
   <input type="hidden" name="a_random_password" value="'.(rand(10,100)).'">
   <input type="submit" value="submit" name="submit_btn">
</form>';

}

if(isset($_REQUEST['submit_btn']))
    {
       $user_name= $_POST["user_name"];
       $a_random_password = $_POST["a_random_password"];

       echo "Your User Name is:". $user_name;
       echo "<br> and your Password is : $a_random_password;

    }

?>

And my post.php

<?php
$url = "http://test.com/form.php";

$data = array(
'user_name' => 'John',
'a_random_password' => 'xxx',
'submit' => 'submit'
);

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',    
        'content' => http_build_query($data)        
    )
);

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { echo "Error"; }

var_dump($result);

?>

So how do i get the a_random_password value and submit it along with the form in a single request, else the password wont fit with the user name.

Google
  • 23
  • 1
  • 10
  • you need to scrape it first – Lawrence Cherone Oct 31 '20 at 21:14
  • @LawrenceCherone i tried to download the form using curl, but by the time i upload the form, the password is changed since i request twice the form. – Google Oct 31 '20 at 21:18
  • you wouldn't request the form twice, use curl to do a http request to form.php with cookie jar (as they would be storing the random token in session.. its actually called a CSRF token), then parse the page for the *password* with domdocument, and dont close the curl connection, then simply make another which directly posts to post.php.. good luck I generally don't answer scraping questions – Lawrence Cherone Oct 31 '20 at 21:34

2 Answers2

0

Sup, what you wanna do is impossible using PHP in that way, cause the only job PHP has is to render your content on server side. To listen to the client side you'll need to use javascript.

    let randomPassword = document.getElementById('a_randow_password')
    let name = document.getElementById('name')
    let password = document.getElementById('password')
    
    name.onkeyup = function(){
        password.value = randomPassword.value + name.value
    }
<input type="text" id="name" name="user_name" placeholder="name"><br />
<input type="hidden" name="a_random_password" id="a_randow_password" value="xxx">
<input type="text" id="password" placeholder="password"><br />
andre_luiss
  • 135
  • 1
  • 5
  • Oh, that's a slap in the face : / I imagine that i could "Call the form and ask the random password, and give (post) my user name along with the password before i hang up" – Google Oct 31 '20 at 21:36
  • haha you CAN do that, and you CAN DO THAT WITH PHP, by using `AJAX`, wich will make a request on your backend without reloading the page. But we only use it if it's something that we are pulling from a database or other complicated stuff. I highly recommend you to try to pass this value to javascript. – andre_luiss Oct 31 '20 at 21:45
-1

Not sure exactly what you trying to achieve, but if You want to interrupt a request (and modify|use it) from HTML to PHP, You need AJAX request (JavaScript).

lohe
  • 109
  • 11