I have made a quick $wpdb->insert
function that adds content with an AJAX function.
The goal is to insert the form data into a table.
Here is my Javascript function calling the ajax:
$(".modal__form").submit(function(e){
e.preventDefault();
$.post(
ajaxurl,
{
'action': 'add_to_newsletter',
'param': $(this).serialize()
},
function(response){
if(response == 'success') {
$('.modal__form').hide();
$('.modal__box .text__success').show();
}
}
);
});
The form data in headers can be printed as :
action: add_to_newsletter
param: firstname=FIRSTNAME&lastname=LASTNAME&email=EMAIL%40DOMAIN.COM&type=TYPE
On my function, I have this :
function add_to_newsletter() {
global $wpdb;
$table = $wpdb->prefix.'newsletter';
$param = $_POST['param'];
if($wpdb->insert($table, array(
'firstname' => $param['firstname'],
'lastname' => $param['lastname'],
'email' => $param['email'],
'ip' => $_SERVER['REMOTE_ADDR']
),
array( '%s', '%s', '%s', '%s')
)) {
var_dump($wpdb);
echo 'success';
}
else {
$wpdb->print_error();
}
die();
}
add_action( 'wp_ajax_add_to_newsletter', 'add_to_newsletter' );
add_action( 'wp_ajax_nopriv_add_to_newsletter', 'add_to_newsletter' );
The problem is that I only got strange things on the database when posted.
Here is the var_dump()
return.
"INSERT INTO 'actz_newsletter' ('firstname', 'lastname', 'email', 'ip') VALUES ('f', 'f', 'f', '::1')"
Anyone ever expected something similar? I am clueless about my research.
I have made a quick $wpdb->insert
function that adds content with an AJAX function.
The goal is to insert the form data into a table.
Here is my Javascript function calling the ajax:
$(".modal__form").submit(function(e){
e.preventDefault();
$.post(
ajaxurl,
{
'action': 'add_to_newsletter',
'param': $(this).serialize()
},
function(response){
if(response == 'success') {
$('.modal__form').hide();
$('.modal__box .text__success').show();
}
}
);
});
The form data in headers can be printed as :
action: add_to_newsletter
param: firstname=FIRSTNAME&lastname=LASTNAME&email=EMAIL%40DOMAIN.COM&type=TYPE
On my function, I have this :
function add_to_newsletter() {
global $wpdb;
$table = $wpdb->prefix.'newsletter';
$param = $_POST['param'];
if($wpdb->insert($table, array(
'firstname' => $param['firstname'],
'lastname' => $param['lastname'],
'email' => $param['email'],
'ip' => $_SERVER['REMOTE_ADDR']
),
array( '%s', '%s', '%s', '%s')
)) {
var_dump($wpdb);
echo 'success';
}
else {
$wpdb->print_error();
}
die();
}
add_action( 'wp_ajax_add_to_newsletter', 'add_to_newsletter' );
add_action( 'wp_ajax_nopriv_add_to_newsletter', 'add_to_newsletter' );
The problem is that I only got strange things on the database when posted.
Here is the var_dump()
return.
"INSERT INTO 'actz_newsletter' ('firstname', 'lastname', 'email', 'ip') VALUES ('f', 'f', 'f', '::1')"
Anyone ever expected something similar? I am clueless about my research.
Share Improve this question edited Jun 5, 2019 at 17:36 Mayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges asked Jun 5, 2019 at 16:14 Patrice PoliquinPatrice Poliquin 3041 gold badge4 silver badges18 bronze badges 1 |1 Answer
Reset to default 3$_POST['param']
is a string, you can not treat it like an array. That's why only f
characters are inserted.
You have two options, prepare data as an array in javascript or parse the string to array on the PHP side.
Sample code for 2nd option:
$table = $wpdb->prefix.'newsletter';
// 'firstname=FIRSTNAME&lastname=LASTNAME&email=EMAIL%40DOMAIN.COM&type=TYPE'
$input = explode( '&', $_POST['param'] );
$param = array_reduce( $input, function( &$carry, $item ) {
$parts = explode('=', $item);
$carry[ $parts[0] ] = urldecode( $parts[1] );
return $carry;
},
array()
);
wp_json_encode
the parameters instead of serializing. Then you need to decode once you receive them. – MikeNGarrett Commented Jun 5, 2019 at 17:51