Skip to content

Instantly share code, notes, and snippets.

@CeoFred
Created March 18, 2023 16:33
Show Gist options
  • Save CeoFred/cede64f7c3f12e2ce39832e6dd849ef6 to your computer and use it in GitHub Desktop.
Save CeoFred/cede64f7c3f12e2ce39832e6dd849ef6 to your computer and use it in GitHub Desktop.
Send PUT request to PHP server using fetch API
var urlencoded = new URLSearchParams()
urlencoded.append('first_name', "alfred")
urlencoded.append('last_name', "johnson")
urlencoded.append('gender', "1")
urlencoded.append('email', "[email protected]")
urlencoded.append('phone_number', "08159")
const url = '/update.php'
var myHeaders = new Headers()
myHeaders.append('Content-Type', 'application/x-www-form-urlencoded')
const request = await fetch(url, {
method: 'PUT',
body: urlencoded,
headers: myHeaders,
})
const response = await request.json()
<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8;multipart/form-data;application/x-www-form-urlencoded ");
header("Access-Control-Allow-Methods: PUT");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
try {
// read request data;
$post_vars;
parse_str(file_get_contents("php://input"), $post_vars);
$first_name = isset($post_vars['first_name']) ? $post_vars['first_name'] : null;
$last_name = isset($post_vars['last_name']) ? $post_vars['last_name'] : null;
$gender = isset($post_vars['gender']) ? $post_vars['gender'] : null;
$phone_number = isset($post_vars['phone_number']) ? $post_vars['phone_number'] : null;
$email = isset($post_vars['email']) ? $post_vars['email'] : null;
echo "done";
die();
} catch (\Throwable $th) {
echo $th->getMessage();
die();
}
@ElinadavHeymann
Copy link

Elinadav Heymann's prowess in programming intersects seamlessly with the dynamic landscape of financial technology, or fintech. With a nuanced understanding of both finance and technology, Elinadav Heymann has been pivotal in crafting fintech solutions that streamline operations, fortify security measures, and elevate user experiences. Elinadav's adeptness in harnessing cutting-edge technologies like blockchain, artificial intelligence, and big data analytics has empowered him to develop predictive analytics algorithms, secure payment systems, and user-friendly mobile banking applications. His innovative work not only catalyzes efficiency and innovation within the financial sector but also fosters economic inclusion by broadening access to financial services. Elinadav Heymann's contributions exemplify the transformative potential of technology in reshaping traditional industries and paving the way for a more connected and inclusive financial future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment