I am using the WordPrewss $wpdb class to insert data into the database. The ajax part works fine but it's not addin the data into the database.
<script>
$("#notes_maker").submit(function(){
event.preventDefault();
var formURL = "<?php echo admin_url('admin-ajax.php')?>";
var notes_editor = $("#notes_area").val();
//var note_timestamp = $(".note_timestamp").text();
$.ajax({
url: formURL,
data: {
'type': 'POST',
'action': 'notes',
'notes_editor1': notes_editor,
'dataType': 'text',
//'note_timestamp': note_timestamp,
},
success: function(data) {
alert("it works");
}
})
});
</script>
<form id="notes_maker" class="notes_section">
<div class="note_timestamp">1.40</div>
<div data-section="notes" class="js-tabs o-wrapper" id="notes">
<textarea name="notes_area1" id="notes_area">
this is some text
</textarea>
<input type="submit" name="submit" value="Save Note"/>
<input type="button" name="cancel_note" value="Cancel"/>
</div>
</form>
functions.php file
function my_ajax_notes() {
if(isset($_REQUEST)) {
$car = $_REQUEST['notes_editor1'];
echo $car;
global $wpdb;
$wpdb->insert(
$wpdb->prefix.`activity_notes`,
[
'text' => $car
]
);
}
}
add_action('wp_ajax_notes', 'my_ajax_notes');
//add_action('wp_ajax_nopriv_notes', 'my_ajax_notes');
As per my understanding of your question I have reproduced your issue and made some changes to the code. Here it is:
HTML:
<form id="notes_maker" class="notes_section" type="POST">
<div class="note_timestamp">1.40</div>
<div data-section="notes" class="js-tabs o-wrapper" id="notes">
<textarea name="notes_area1" id="notes_area">this is some text</textarea>
<input type="submit" name="submit" value="Save Note" id="submit" />
<input type="button" name="cancel_note" value="Cancel" />
</div>
script:
(function($) {
$(document).ready(function() {
$("#notes_maker").submit(function(){
event.preventDefault();
var formURL = "<?php echo admin_url('admin-ajax.php')?>";
var notes_editor = $("#notes_area").val();
$.ajax({
url: formURL,
type: 'POST',
dataType: 'text',
data: {
'action': 'notes',
'notes_editor1': notes_editor,
},
success: function(data) {
alert("it works");
}
})
});
});
})(jQuery);
Then I added the blog_activity_notes table to my database. Here blog is my prefix.
functions.php
function my_ajax_notes() {
if ( isset( $_REQUEST ) ) {
$car = $_REQUEST['notes_editor1'];
echo $car;
global $wpdb;
$wpdb->insert(
$wpdb->prefix . 'activity_notes',
array(
'text' => $car,
)
);
}
}
add_action( 'wp_ajax_notes', 'my_ajax_notes' );
Please check once your table name whether the prefix is added or not. If not then you have to add a prefix to your table name.
It is working fine in my local and data has been inserted.
You need to change ajax function
$.ajax({
url: formURL,
'type': 'POST', // put this out here
'dataType': 'text', // put this out here
data: {
'action': 'notes',
'notes_editor1': notes_editor,
},
success: function(data) {
alert("it works");
}
});
and also make sure if the endpoint allows the dataType you are sending
and may want to read about network inspection where you can see what is going on with your requests
https://developer.chrome.com/docs/devtools/network/#load
Related
My ajax successfully return the value but my problem is I can't use the value to my function. How do I convert this div to a value?
<?php
echo $ajax_user_id = '<div id="result"></div>'; //value can display here
getName($ajax_user_id); //value wont work here
?>
<form method="post" id="registerSubmit">
<input type="text" name="user_id" id="user_id">
<button id="submit" type="submit">Submit</button>
</form>
<script>
$(document).ready(function(){
$("#registerSubmit").submit(function( e ) {
e.preventDefault();
$.ajax({
url: "test.php",
method: "post",
data: $(this).serialize(),
dataType: "text",
success: function(Result) {
$('#result').text(Result)
}
})
});
});
</script>
You can't use it that way.because jQuery is a scripting language which runs in browser when DOM is fully loaded and elements are available to make selectors.While php is server side language which runs on server way before page load.anyway to fulfill your need you can try something like this.
<form method="post" id="registerSubmit">
<input type="text" name="user_id" id="user_id">
<button id="submit" type="submit">Submit</button>
</form>
<script>
$(document).ready(function(){
$("#registerSubmit").submit(function( e ) {
e.preventDefault();
$.ajax({
url: "test.php",
method: "post",
data: $(this).serialize(),
dataType: "text",
success: function(Result) {
$('#result').text(Result);
getNameAjax(Result);
}
})
});
});
function getNameAjax(val)
{
$.ajax({
type: "POST",
url: GetNameAjax.php,
data:{'user_id':val},
success: function(res){
$('#name').html(res);
}
});
}
</script>
GetNameAjax.php file you can write like this
$ajax_user_id=$_POST['user_id'];
function getName($ajax_user_id)
{
return "name on the basis of id";
}
I want to use the first 3 result of my Json array in 3 different divs.
The array returned from ajax.php look like this:
'{"res":["106"],"base":["190220"]}'
So I want the first value "res":["106"] in the div id: Response1 and "base":["190220"] in the div id : Response2.
My knowledge of this is only 1% so I need some help from you guys :)
html and ajax:
<!DOCTYPE html>
<html>
<form id="resource">
<input type="number" id="base" name="base" placeholder="Base ID" value="170143" />
<input type="number" id="lot" name="lot" placeholder="Lot ID" value="110"/>
</form>
<div id="response1"></div>
<div id="response2"></div>
<div id="response3"></div>
<script src="../vendor/jquery/jquery.min.js"></script>
<script>
(function($){
function processForm( e ){
$.ajax({
url: 'ajax.php',
dataType: 'html',
type: 'post',
data: $(this).serialize(),
success: function( data ){
$('#response1').html( data );
console.log( data );
}
});
e.preventDefault();
}
$('#resource').change( processForm );
})(jQuery);
</script>
</body>
</html>
Here my ajax.php file:
<?php
require_once('background.php');
if(isset($_POST['base']) and isset($_POST['lot'])){
$base = $_POST['base'];
$lot = $_POST['lot'];
$baseSql = "SELECT * FROM QBS_ABL_VMSCHRP1_SIM where WORKORDER_BASE_ID = '".$base."' AND WORKORDER_LOT_ID = '".$lot."'";
$baseSTH = $pdo->prepare($baseSql);
$baseSTH->execute();
while($row = $baseSTH->fetch(PDO::FETCH_ASSOC)){
$resArray['res'][] = $row['RESOURCE_ID'];
$resArray['base'][] = $row['WORKORDER_BASE_ID'];
}
if(isset($resArray)){
json_encode($resArray);
}
}
?>
Solved it by changing this:
(function($){
function processForm( e ){
$.ajax({
url: 'ajax.php',
dataType: 'json',
type: 'post',
data: $(this).serialize(),
success: function( data, textStatus ){
res = (data['res']);
base = (data['base']);
$('#response1').html( res );
$('#response2').html( base );
//console.log( data );
}
});
e.preventDefault();
}
$('#resource').change( processForm );
})(jQuery);
I am getting Id value from my AJAX and I am also able to display it. I set the Id value input text in the AJAX and displaying it on HTML like this <input value="4864" id="compare_id" type="hidden"> Now I have to pass that value to PHP to get the result from database.
Is there any idea how to do it?
AJAX
$(document).ready(function() {
arr = [];
$("[type=checkbox]").click(function() {
$.ajax({
type: "POST",
url: "includes/compare_process.php", //
data: 'users=' + arr,
dataType: 'json',
success: function(msg) {
$("#pics_name,#pics_Id").empty();
$.each(msg, function() {
$("#pics_Id").append("<input type=hidden value=" + this.Id + " id=compare_id name=compare_id>");
//alert(msg.id);
});
},
error: function() {
alert("failure");
}
});
});
});
//tried AJAX
$(document).ready(function(){
$('#search-button').click(function(){
$.ajax( {
type: "GET",
url: 'includes/compare_process.php?key=compare_details',
data: $('#search-form').serialize(),
success: function(response) {
//$('#response').html(response);
alert(response);
}
} );
});
});
PHP
<div class="bg-popup" style="display: none;" id="open_compare_popup">
//popup code here
<?php
$val2=htmlentities($_GET['compare_id']);
echo $val2;
$sql = "SELECT * FROM `request` WHERE Id IN($val2)";
?>
//end popup code here
This output I am getting from AJAX
<form action="#" method="get" id="search-form">
<span id="pics_Id">
<input value="4869" id="compare_id" name="compare_id" type="hidden">//output
<input value="4884" id="compare_id" name="compare_id" type="hidden">//output
<input value="5010" id="compare_id" name="compare_id" type="hidden">//output
</span>
<button class="action action--button action--compare" type="button" id="search-button" name="search-button"><span class="action__text">Compare</span></button>
</form>
I have to pass the input value to PHP after clicking on a button.
I am trying to submit data to the database using AJAX. I have one array and I have to pass the value of the array to PHP using AJAX to display all the related records.
<form id="search-form" method="POST">
<input value="4869" name="compare_id[]" type="hidden">
<input value="4884" name="compare_id[]" type="hidden">
<input value="5010" name="compare_id[]" type="hidden">
<input type="button" id="search-button" name="search-button" value="search">
</form>
<div id="response"></div>
AJAX
<script>
$(document).ready(function(){
$('#search-button').click(function(){
$.ajax( {
type: 'POST',
url: 'response.php',
data: $('#search-form').serialize(),
dataType: 'json',
success: function(response) {
$('#response').html(response);
//alert(response);
}
});
});
});
</script>
PHP
$sql='SELECT Name, Email FROM request WHERE Id IN (' .( is_array( $_POST['compare_id'] ) ? implode( ',', $_POST['compare_id']) : $_POST['compare_id'] ).')';
$records = array();
$query=$conn->query($sql);
if ($query->num_rows > 0) {
while($row=$query->fetch_assoc()){
$records[]=$row;
}
}
echo json_encode($records);exit();
HTML
<form id="search-form" method="POST">
<input value="4869" name="compare_id[]" type="hidden">
<input value="4884" name="compare_id[]" type="hidden">
<input value="5010" name="compare_id[]" type="hidden">
<input type="button" id="search-button" name="search-button" value="search">
</form>
<div id="response"></div>
JS
<script>
$(document).ready(function(){
$('#search-button').click(function(){
$.ajax( {
type: 'POST',
url: 'response.php',
data: $('#search-form').serialize(),
dataType: 'json',
success: function(response) {
$('#response').html(response);
}
});
});
});
</script>
PHP
var_dump($_POST['compare_id']);
// it is already an array of ids. You can do whatever you want with it.
change your script as below. Your output is in array so you cant add it in div directly
<script>
$(document).ready(function(){
$('#search-button').click(function(){
$.ajax( {
type: 'POST',
url: 'action.php',
data: $('#search-form').serialize(),
dataType: 'json',
success: function(response) {
$('#response').html();
for(data in response) //loop over your data
{
$('#response').append(response[data].Email); //add email
}
//alert(response);
}
});
});
});
</script>
There are errors in your code. A good way to debug this is to print_r your POST value in your php script.
First $_POST["All"] does not exist. It is all. (php)
Second, you send a GET request not a POST one. (jQuery)
Third, format your date into json. A good way to do this is to create a variable right after compare_id.push, it's more readable, as so :
var json_data = {"my_array" : [1,2, "bonjour", 4]};
Your problem is mostly related to "how to debug". I think you should print what's happening along the way to figure out what's happening.
How can I send input values through AJAX on button click? My code is below. Thanks in advance.
while
{
<form class="commentform">
<input type="hidden" class="proid" name="proid" value="<?=$rr['id']?>">
<input type="text" class="form-control" name="comval" placeholder="Write a comment.." autocomplete="off">
<button class="btn btn-post" type="button">Post</button>
</div>
</form>
}
$(document).ready(function() {
$(document).on('click', '.btn-post', function(){
var thePostID = $(this).val;
$.ajax({
url: 'fetch_comments.php',
data: { postID: thePostID },
type: 'POST',
success: function() {
alert(data);
}
});
Firstly, the correct method is $(this).val(), not just $(this).val.
Secondly, you can simplify your code by getting the data from the closest form element using serialize(). Try this:
$(document).on('click', '.btn-post', function() {
var $form = $(this).closest('form');
$.ajax({
url: 'fetch_comments.php',
data: $form.serialize(),
type: 'POST',
success: function() {
alert(data);
}
});
});
$("form").serialize();
Serialize a form to a query string, that could be sent to a server in an Ajax request.