Send Form Data Directly to API

Asked

Viewed 1,500 times

0

Hello ! I have a simple php registration with some customer data, I need to send this data to an API with POST of an PR of our company and record this data in the database too, I’m beginner and I’m picking up a lot

  • You want to send data from a form to an external API and save to your own database?

  • Yes, I have until the link of the site here https://erp.bluesoft.com.br/api/#/core/incluUsingPOST_1

  • I am using PHP

  • my data is already being saved in the database !

  • I’ll suggest you research on ajax.

1 answer

1


I’ll add a basic example of how you’re likely to get your data sent to an API and saved to your database:

//ESTE SCRIPT FOI FEITO UTILIZANDO A BIBLIOTECA JQUERY
	//LEMBRE-SE DE ADICIONA-LA NO HEAD DA PÁGINA

	//EVENTO DE CLICAR NO SUBMIT
	$("#send").click(function(event){
		
		//EVITAR QUE O FORMULÁRIO SEJA SUBMETIDO ANTES DO ENVIO PARA A API
		event.preventDefault();

		//ENVIA PARA A API
		$.ajax({
			//METODO DE ENVIO
			type: "POST",
			//URL PARA QUAL OS DADOS SERÃO ENVIADOS
			url: "erp.bluesoft.com.br/beta/api",
			//DADOS QUE SERÃO ENVIADOS
			data: $("#formulario").serialize(),
			//TIPOS DE DADOS QUE O AJAX TRATA
			dataType: "json",
			//CASO DÊ TUDO CERTO NO ENVIO PARA A API
			success: function(){
				//SUBMETE O FORMULÁRIO PARA A ACTION DEFINIDA NO CABEÇALHO
				$("#formulario").submit();
			}
		});
	})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
	<form id="formulario" method="POST" action="insert.php">
    <label>nome</label>
		<input type="text" name="nome"></br>
	  <label>sobrenome</label>
    <input type="text" name="sobrenome"></br>
		<label>bairro</label>
    <input type="text" name="bairro"></br>
		<label>CEP</label>
    <input type="text" name="cep"></br>
		<input type="submit" id="send" value="Enviar">
	</form>
</body>
</html>

  • I have to send the data via Curl, ta osso Mans kk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.