I would like to pass two array data to php and extract them in two prefix so I can choose them by that two prefix.
Example code
HTML
<form>
<!-- set one -->
<div class="one">
<input id="name" name="name">
</div>
<!-- /set one -->
<!-- set two -->
<div class="two">
<input id="email" name="email">
</div>
<!-- /set two -->
<!-- set one again -->
<div class="one">
<input id="msg" name="msg">
</div>
<!-- /set one -->
<button type="submit"></button>
</form>
Jquery
I don't know how to pass set one and set two in data, should it pass by two array? I would like to select them by parent class one and two.
$('form').on('submit', function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'contact.php',
data: $('form').serialize(),
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data);
}
});
});
PHP
<?php
extract($_POST, EXTR_PREFIX_ALL, 'one'); // how to extract only set one?
extract($_POST, EXTR_PREFIX_ALL, 'two'); // how to extract only set two?
>
I want to use them like this in php:
if (empty($one_name) || empty($two_email) || empty($one_msg)) { // select them by prefix
// function
}
The html can not include php code, how can use jquery and php do that?
Thanks.