message WhatsApp without saving the contact php

To send a message through WhatsApp without saving the contact’s phone number to your phone’s contact list, you can use the following form:

  1. Add an input field for the phone number and a submit button to your HTML form:
<form method="post" id="formulario" class="form">
  <div class="form_box">
    <div class="div_phone">
      <input type="tel" name="phone_number" id="phone_number" placeholder="(00) 0000-0000"><br>
    </div>
    <div class="div_submit">
      <input type="submit" class="submit">
    </div>
  </div>
</form>
  1. In your PHP code, check if the phone number has been submitted through the form. If it has, use the preg_replace function to remove any special characters from the phone number, such as parentheses, dots, or dashes.
if (array_key_exists("phone_number", $_POST)) {
  $a =  $_POST["phone_number"];
  $arr = array('/\(|\)/','/\.|\-/');
  $cellphone = preg_replace($arr, "", $a);
}
  1. Use the empty function to check if the phone number is empty. If it is, display an error message to the user.
if (empty($cellphone)) {
  echo '<p class="err_text">The phone number cannot be empty!</p>';
}
  1. If the phone number is not empty, create a URL with the following format: https://api.whatsapp.com/send?1=pt_BR&phone=55<PHONE NUMBER>&text=<MESSAGE>. Replace <PHONE NUMBER> with the phone number submitted through the form and <MESSAGE> with the message you want to send.
$whats = "https://api.whatsapp.com/send?1=pt_BR&phone=55";
$message = "&text=Hello, I am using WhatsApp";
$link = "{$whats}{$cellphone}{$message}";
  1. Use the header function to redirect the user to the WhatsApp web page with the URL you created.
header("Location: {$link}");

This will open the WhatsApp web page with the phone number and message pre-filled, allowing the user to send the message without saving the contact’s phone number to their phone’s contact list.

Deixe um comentário