Когда пользователь нажимает кнопку «Отправить», я хочу отправить запрос POST на специальный URL-адрес, который был создан на основе информации в форме.Я также хочу, чтобы он вставил строку в базу данных WordPress MYSQL.
Я добавил в конец admin-ajax.php
:
function add_query_db_callback(){
global $wpdb;
$id_instagram = $_POST['id_instagram'];
$table_name = 'wp_insta_email';
$data_array = array(
'id_instagram' => $id_instagram
);
$rowResult = $wpdb->insert($table_name, $data_array, $format=NULL);
echo $_POST['data'];
if (!$rowResult) {
echo "FAILED TO UPDATE";
} else {
$rowResult;
echo "WILL UPDATE SUCCESSFULLY - CALL RESULT FUNCTION";
};
wp_die();
die();
}`
и в файле header.php
:
<!-- Search_box -->
<div class="s006">
<form method="post" id="form_id" onsubmit="return myFunction()">
<fieldset>
<div class="inner-form">
<div class="input-field">
<button class="btn-search" type="button submit" value="Submit" name="BtnSubmit">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24">
<path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
</svg>
</button>
<input id="search" type="text" placeholder=" INPUT YOUR ID INSTAGRAM " value="" name="id_instagram"/>
</div>
</div>
</fieldset>
</form>
</div>
и
<script type="text/javascript">
function myFunction() {
var act= "//app.neolyze.com/public/"+document.getElementById("search").value;
var name = document.getElementById("search").value;
document.getElementById("form_id").action = act;
document.getElementById("form_id").submit();
if (name == '') {
alert("Please Fill All Fields");
} else {
// AJAX code to submit form.
var ajaxData = {
'action': 'add_query_db',
'id_instagram': name
}
jQuery.ajax({
type: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: ajaxData,
success: function( response ) {
console.log("Data returned: " + response );
$statusSelectCell.parent().css({"background-color": "#b3e6b3"});
$statusSelectCell.parent().animate({backgroundColor: currentBackgroundColor}, 1200);
},
error: function() {
alert("FAILED TO POST DATA!!");
}
});
}
return act;
}
</script>
, но он не сохраняется в моей базе данных (только переход на мой предполагаемый URL).