Jump to content

Send POST data without a form


Recommended Posts

How can I send POST data without using a form?

 

For instance, when process.php loads, I want it to load an entry from the DB, and sent it to receive.php as a POST variable.

 

Maybe it can be done with CURL, but I'm not familiar with that so idk. Suggestions, tips, tutorials.. anything is welcome.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/104138-send-post-data-without-a-form/
Share on other sites

Based on my limited knowledge you cannot send a $_POST variable without a form, but you may want to look into the hidden input type to solve your problem.

 

<input type=hidden name=data value='datayouwanttosend'>

 

or you could set a temporary cookie to send the data, then once its recevied destroy the cookie.

I did find this on google... but not sure how to use it lol maybe one of the SR. guys can help out

 

Answer :

You can open an HTTP socket connection and send HTTP POST commands. Here is

an example :

 

<? 
// Generate the request header 
$ReqHeader = 
"POST $URI HTTP/1.1\n". 
"Host: $Host\n". 
"Content-Type: application/x-www-form-urlencoded\n". 
"Content-Length: $ContentLength\n\n". 
"$ReqBody\n"; 

// Open the connection to the host 
$socket = fsockopen($Host, 80, &$errno, &$errstr); 
if (!$socket) 

$Result["errno"] = $errno; 
$Result["errstr"] = $errstr; 
return $Result; 
} 
$idx = 0; 
fputs($socket, $ReqHeader); 
while (!feof($socket)) 

$Result[$idx++] = fgets($socket, 128); 
} 
//------------------------------------------- 
?>  

Take a look at the curl extension.

 

Thanks, I have this now.

 

<?php
// Redirect
$curl_session = @curl_init('./receive.php');
@curl_setopt($curl_session, CURLOPT_POST, 1);
@curl_setopt($curl_session, CURLOPT_POSTFIELDS, "name=$username");
@curl_setopt($curl_session, CURLOPT_FOLLOWLOCATION, 1);
$send = @curl_exec($curl_session);

if (!send)
{
  // well.. didnt work
}
?>

 

 

The code doesn't redirect properly... Any help?

 

Wes

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.