post php form code example
Example 1: post request php
$response = httpPost("http://mywebsite.com/update.php",
array("first_name"=>"Bob","last_name"=>"Dillon")
);
//using php curl (sudo apt-get install php-curl)
function httpPost($url, $data){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Example 2: how to access form data usinf method get
// Check if the form is submitted
if ( isset( $_POST['submit'] ) ) {
// retrieve the form data by using the element's name attributes value as key
echo 'form data retrieved by using the $_REQUEST variable'
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
// display the results
echo 'Your name is ' . $lastname .' ' . $firstname;
// check if the post method is used to submit the form
if ( filter_has_var( INPUT_POST, 'submit' ) ) {
echo 'form data retrieved by using $_POST variable'
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
// display the results
echo 'Your name is ' . $lastname .' ' . $firstname;
}
// check if the get method is used to submit the form
if ( filter_has_var( INPUT_GET, 'submit' ) ) {
echo 'form data retrieved by using $_GET variable'
$firstname = $_GET['firstname'];
$lastname = $_GET['lastname'];
}
// display the results
echo 'Your name is ' . $lastname .' ' . $firstname;
exit;
}