I use form for input date to DB. After submit form I need refresh php code (because when form is submit then I need display this value sent, instead of this form.)
I've script:
$(document).on('submit','#form_create_user',function(e){
e.preventDefault();
var fd = new FormData(this);
var obj = $(this);
fd.append('course_id', "<?php echo $this->uri->segment(4); ?>");
obj.find('input[type="submit"]').val("Tworzenie...")
$.ajax({
url: $(this).attr("action"),
data: fd,
cache: false,
processData: false,
contentType: false,
type: 'POST',
success: function (dataofconfirm) {
// do something with the result
// obj.find('input[type="submit"]').val("Confirm user")
}
});
$.ajax({
url: "<?php echo site_url('home/saveValues/'); ?>",
data: fd,
cache: false,
processData: false,
contentType: false,
type: 'POST',
success: function (dataofconfirm) {
// do something with the result
toastr.success("Success created user.");
obj.find('input[type="submit"]').val("Potwierdź")
}
});
})
I can implement to this code refresh page below script:
document.getElementById("form_create_user").onsubmit = function(){
window.location.replace("<?php echo $course_details_url_back; ?>");
}
But the problem is, I have main tab and togle switch tabs with end url #user #domain etc.
example:
- mydomain.com/course
- mydomain.com/course#user
- mydomain.com/course#domain
When I run url in browser: mydomain.com/course#user then still is displayed main tab with url mydomain.com/course
This working only on switch tabs (without directly run url) So when I use above solution this stil reload page and back to main mydomain.com/course
So I need to find any solution for implement to above script refresh php code without reload page. Can anyone help me?
Related
PHP variable:
<?php $course_details_url_back = site_url("home/lesson/".slugify($course_details['title'])."/".$course_id); ?>
script do action for form:
$(document).on('submit','#form_create_user',function(e){
e.preventDefault();
var fd = new FormData(this);
var obj = $(this);
fd.append('course_id', "<?php echo $this->uri->segment(4); ?>");
obj.find('input[type="submit"]').val("Tworzenie...")
$.ajax({
url: $(this).attr("action"),
data: fd,
cache: false,
processData: false,
contentType: false,
type: 'POST',
success: function (dataofconfirm) {
// do something with the result
// obj.find('input[type="submit"]').val("Confirm user")
}
});
$.ajax({
url: "<?php echo site_url('home/saveValues/'); ?>",
data: fd,
cache: false,
processData: false,
contentType: false,
type: 'POST',
success: function (dataofconfirm) {
// do something with the result
toastr.success("Success created user.");
obj.find('input[type="submit"]').val("Potwierdź")
}
});
})
After submit form I need do above actions and also I need to implement to above code refresh page on submit form and back to url.
For this I've separate code:
document.getElementById("form_create_user").onsubmit = function(){
window.location.replace("<?php echo $course_details_url_back; ?>");
}
But can anyone help me implement this to current one code?
I want to upload a file using jQuery/Ajax in my Wordpress plugin. The javascript to PHP call works. So the wiring etc. works. But as soon as I post the formData, necessary to post the files, I don't reach the PHP function any more.
The javascript,
var doesntWork = new FormData();
doesntWork.append('file', 'a name');
var withthisItWorks = 'text'
var data = {
'action': 'emfi_file_upload',
'data': doesntWork
}
$.ajax({
type: "POST",
url: ajax_object.ajaxurl,
data: data,
success: function(response) {
jQuery('#emfi-message').html(`<span style="color: green;">Respons: ${response}</span>`);
}
});
The PHP function just returns a string answer:
function emfi_file_upload_callback() {
echo 'Yes, in the callback';
wp_die();
}
When I put the plain text in my data object I get answer from the PHP function. When I put the formData in, there is no answer. I've tried a lot of examples on the internet, but it boils down to this every time. Adding contentType: false and processData: false, as was mentioned somewhere else, didn't help. What's wrong?
All fields that are being sent must be in the formdata object.
Additionally for FormData to work with jQuery.ajax, processData and contentType have to be set to false.
var doesWork = new FormData();
doesWork.append('file', someFile);
doesWork.append('action', 'emfi_file_upload');
$.ajax({
type: "POST",
url: ajax_object.ajaxurl,
data: doesWork,
contentType: false,
processData: false,
success: function(response) {
jQuery('#emfi-message').html(`<span style="color: green;">Respons: ${response}</span>`);
}
});
I am trying to pass data to php code using ajax. but I cant get is success.
<?php
$message = $_POST["message"];
$buyer_name = $_POST["buyer_name"];
$order_number = $_POST["order_number"];
$account = $_POST["account"];
$designer = $_POST["designer"];
echo $message; ?>
this is my js code.
var formdata = {buyer_name:byrName,order_number:orderNum.trim(),account:account,designer:'admin',message:'testing'}
if(autoMode){
$.ajax({
type:'POST',
url: 'msgHandle.php',
data: formdata,
contentType: false,
cache: false,
processData: false,
beforeSend: function() {
},
success: function(data) {
alert(data);
},
error: function() {
alert('failed');
}
});
I have set a button to click and when clicked in runs this ajax code. the output is a alert with empty message. that means it success but seems like variables does not pass correctly to the php code. what is wrong in my code, I can't find.
I tried your code and found that by removing all the three parameters
contentType: false,
cache: false,
processData: false,
From the code posts your data onto another page.
Tried for just a sample array.
This should be a really simple fix, but I am pulling my hair out.
This part of the site is really simple. I have a .js file that uses ajax to insert a post into the database when submit is clicked. I get the success part of the jquery, but nothing ever gets put into the database.
Here is the jquery in the $(document).ready(function():
//Button for profile post
$('#submit_profile_post').click(function(){
$.ajax({
type: "POST",
url: "includes/handlers/ajax_submit_profile_post.php",
data: $('form.profile_post').serialize(),
success: function(msg) {
$("#post_form").modal('hide');
location.reload();
},
error: function() {
alert('Failure');
}
});
});
Here is the ajax handler:
<?php
require '../../config/config.php';
include("../classes/User.php");
include("../classes/Post.php");
include("../classes/Notification.php");
if(isset($_POST['post_body'])) {
$post = new Post($con, $_POST['user_from']);
$post->submitPost($_POST['post_body'], $_POST['user_to']);
}
?>
As I said, If I add an alert under the jquery success, I get it just fine but nothing happens after that.
Any help would be greatly appreciated!
Try using FormData
var fd = new FormData();
fd.append('field', value);
$.ajax({
url: 'http://example.com/script.php',
data: fd,
processData: false,
contentType: false,
type: 'POST',
`enter code here`
success: function(data) {
alert(data);
}
});
Here is my Javascript for fetching value to input box,
$('#edituserModal').on('show.bs.modal', function(e) {
var userid = $(e.relatedTarget).data('userid');
var u_id = document.getElementById('hdn_user_id').value = userid;
alert(userid);
});
I want this value to use for SQL query in modal window which is on same page.
I fetched value in modal window but unable to use it. What the format to use it.
You can pass js variable into php page using ajax.
$('#edituserModal').on('show.bs.modal', function(e) {
var userid = $(e.relatedTarget).data('userid');
var u_id=document.getElementById('hdn_user_id').value=userid;
$.ajax({ //create an ajax request to load page.php
type: "GET",
url: "page.php",
data:"varabletophp="+u_id, //Here is the value you wish to pass in to php page
dataType: "html", //expect html to be returned
success: function(response){
alert(response);
}
});
});
No you can get this variable into your page.php (php page) using
$fromjs=$_GET['varabletophp'];
echo $fromjs;
you can use input hidden and set userid value in on this input so , post form
Varying modal content based on trigger button :
http://getbootstrap.com/javascript/#modals-related-target
var data = new FormData($('form#' + formId)[0]);
$.ajax({
type: "POST",
url: url,
data: data,
cache: false,
processData: false,
contentType: false,
beforeSend: function () {
},
success: function (response) {
},
error: function (response) {
}
});