html form POST_ not populating DB/ email


I have read quite a few advice on the subject in the forum but none of the methods worked for me.
I have included an HTML form into a page template.

        <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.0.js"></script>
        <script type="text/javascript">
          const url = "https://www.plasticatbay.org/ChartBal/populatebeach2.php";
          $.getJSON(url, function(data){
            $('#selector').empty();
            $('#selector').append('<option selected="true" disabled>Choose a beach</option>')
            $('#selector').prop('selectedIndex', 0);
            $.each(data, function(key,entry){
              $('#selector').append($('<option>').text(entry.Beach).attr('value', entry.Id))});
            });
          </script>
        <script type="text/javascript">
        function validateForm(){
          /* Validating name field */
          var x=document.forms["myForm"]["name"].value;
          if (x==null || x=="") {
            alert("Name must be filled out");
            return false;
            }
          /* Validating email field */
          var x=document.forms["myForm"]["email"].value;
          var atpos=x.indexOf("@");
          var dotpos=x.lastIndexOf(".");
          if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
            alert("Not a valid e-mail address");
            return false;
            }
          var x=document.forms["myForm"]["date"].value;
          if (x==null || x=="") {
            alert("The date must be filled out");
            return false;
            }
          var x=document.forms["myForm"]["selector"].value;
          if (x==null || x=="") {
            alert("The beach must be selected");
            return false;
            }
          var x=document.forms["myForm"]["weight"].value;
          if (x==null || x=="") {
            alert("The weight must be provided");
            return false;
            }
        }
          </script>
      </head>
      <body>
      <form action="<?php echo admin_url( 'admin-post.php' ); ?>" method="post" name="myForm">
      <input type="hidden" name="action" value="pollution_data">
      Name:<br> <input id="name" type="text" name="name" /><p>
      Enter your email:<br> <input type = "email" id="email" name="email" /><p>
      Date of collection: <input type="date" id="date" name="date" class="datepicker" /><p>
      Choose a beach: <select id= "selector" name="Choose_beach"></select><br>
      If the beach is not listed please use the contact form<p>
      Weight collected in kg: <input id="weight" type="number" name="weight" /><p>
      Feedback: <br><textarea name="feedback" rows="10" cols="50" ></textarea><p>
      <input type="submit" value="Submit" /></form>
      </body>

I have modified the function.php to export my POST data via admin_post but nothing seems to work. Here an exert of the function.

/**
 * Manage form
 */
add_action( 'admin_post_nopriv_pollution_data', 'add_pollution_data' );
add_action( 'admin_post_pollution_data','add_pollution_data');
function add_pollution_data(){	
	$name=$email=$date=$beach=$weight=$unit=$feedback="";
//get the form elements and store them in a variable
  
	if (! is_page("contribute-to-marine-pollution-data-collection")||! isset( $_POST['name'] )) {
		return;}
 	 $name=test_input($_POST["name"]);
 	 $email=test_input($_POST["email"]);
 	 $date=test_input($_POST["date"]);
 	 $beach=test_input($_POST["beach"]);
 	 $weight=test_input($_POST["weight"]);
 	 $feedback=test_input($_POST["feedback"]);
	
	function test_input($data) {
	  $data = trim($data);
 	 $data = stripslashes($data);
 	 $data = htmlspecialchars($data);
	  return $data;
	}
//database
	define('DB_HOST', 'host');
	define('DB_USERNAME', 'user');
	define('DB_PASSWORD', 'pwd');
	define('DB_NAME', 'DB');
//get connection
	$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
	if(!$mysqli){
 	 die("Connection failed: " . $mysqli->error);
 	 }
//insert data
	$query1 ="INSERT INTO <code>TAB1</code> (a,b,c,d) VALUES ($beach, $date,$weight,$email)";
	$result1 = $mysqli->query($query1);
//free memory associated with result
$result1->close();
//close connection
$mysqli->close();
// Sanitize E-mail Address
	$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
	$email= filter_var($email, FILTER_VALIDATE_EMAIL);
	if (!$email){
 	 echo "Invalid Sender's Email";
 	 }
	$subject ="Data Submission";
	$headers ='From: databot@plasticatbay.org.rn';
	$message = wordwrap("name:t$namenemail:t$emailndate:t$datenbeach:t$beachnWeigth:t$weight $unitnmsg:n",70);
	wp_mail("julien.moreau@plasticatbay.org",$subject,$message,$headers);
	echo "Thanks for the submission!";
	//Redirects to the success page
	header("Location: https://www.plasticatbay.org/contribute-to-marine-pollution-data-collection");
}

I don’t think the function is read and I don’t really find a way of debugging this. Any help is welcome.

The page I need help with: [log in to see the link]



Source link