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?
Related
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?
Data is not display after insert through bootstrap modal with ajax in php mysqli.
jQuery Ajax code:
$(document).ready(function() {
fetch_data();
function fetch_data() {
var action = "fetch";
$.ajax({
url: 'action.php',
method: 'post',
data: {action: action},
success: function (data) {
$("#all_table_data").html(data);
}
});
}
});
function addData() {
var formData = new FormData($('#employee_insert_form')[0]);
formData.append('action', 'add');
$.ajax({
method: 'post',
url: 'action.php',
data: formData,
processData: false,
contentType: false,
cache: false,
success: function (response) {
$("#all_table_data").html(response);
$('#empInsert').modal('hide');
fetch_data();
}
});
}
Your PHP code never return anything to your view. So try to echo result of select * from tbl_employee after the insertion
<script>
$(document).ready(function(){
$(".submit_modal1").click(function(e){
e.preventDefault();
id = this.id;
name = $("#fname_"+id).val();
email = $("#email_"+id).val();
phone = $("#phone_"+id).val();
upload_resume = $("#upload_resume_"+id).val();
$.ajax({
type:"POST",
data:{"id":id,"name":name,"email":email,"phone":phone,"upload_resume":upload_resume},
url:"<?php echo base_url(); ?>send",
success:function(data){
$("#send_hr").html(data);
}
});
$("#upload_resume_"+id).change(function(){
var file_data = $("#upload_resume_"+id).prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$('#imgsss').html("<img src='<?php echo base_url(); ?>resource/loading.gif' />");
$.ajax({
url: '<?php echo base_url(); ?>upload_resume',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(php_script_response){
$("#imgsss").html(php_script_response);
}
});
});
});
});
</script>
In this code, I trigger two events i.e. click and change where I want to run $("#upload_resume_"+id).change(function(){}); inside $(".submit_modal1").click(function(e){}). Is it possible to do it or is there any other way to trigger both event simulteniously. How can I do this ?Please help me.
Thank You
$("#upload_resume_"+id).change(function(){ does not trigger the event
If you cannot just chain the PHP from the other PHP, then you likely mean
$(function() {
$(".submit_modal1").click(function(e) {
e.preventDefault();
id = this.id;
name = $("#fname_" + id).val();
email = $("#email_" + id).val();
phone = $("#phone_" + id).val();
upload_resume = $("#upload_resume_" + id).val();
$.ajax({
type: "POST",
data: {
"id": id,
"name": name,
"email": email,
"phone": phone,
"upload_resume": upload_resume
},
url: "<?php echo base_url(); ?>send",
success: function(data) {
$("#send_hr").html(data);
$("#upload_resume_" + id).change() // HERE
}
});
$("#upload_resume_" + id).change(); // OR HERE
});
// any change to upload_resume*
$("[id^=upload_resume]").change(function() {
var file_data = $(this).prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$('#imgsss').html("<img src='<?php echo base_url(); ?>resource/loading.gif' />");
$.ajax({
url: '<?php echo base_url(); ?>upload_resume',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(php_script_response) {
$("#imgsss").html(php_script_response);
}
});
});
});
Php does not receive a variable through $ _POST. I'm trying to pass a variable with ajax to a php page, but php takes a variable as NULL. Tell me, what is the error and how to fix it?
jquery code:
var imgPath;
$(".close_modal_clear_cover").on("click", function(e) {
imgPath = $("#cover_preview").attr('src');
$.ajax({
url: "/management/ajax_scripts/img_delete.php",
type: "POST",
data: imgPath,
async: true,
cache: false,
contentType: false,
dataType: "json",
processData: false,
success: function (returndata) {
console.log(imgPath); //url of image
console.log(returndata); // NULL
}
});
});
img_delete.php code:
if (isset($_POST['imgPath'])) {
$path= $_POST['imgPath'];
unlink($path);
$response = $path;
} else {
$response = "false";
}
echo json_encode($response);
data: imgPath should be data: {imgPath: imgPath} and you should change your $_GETs to $_POSTs in the php backend
Thanks #Reflective for the help. Having studied what data comes in img_delete.php and a little googling, I found a solution. At me for some reason contentType was specified as false, but it was necessary 'application / x-www-form-urlencoded; charset = UTF-8 '.
This is a stupid mistake, but suddenly someone with the same collided.
$(".close_modal_clear_cover").on("click", function(e) {
// imgPath = $.session.get("imgCover");
imgPath = JSON.stringify($("#cover_prewiew").attr('src'));
$.ajax({
url: "/management/ajax_scripts/img_delete.php",
type: "POST",
data: {imgPath: imgPath},
async: true,
cache: true,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
dataType: "json",
processData: true,
success: function (returndata) {
console.log(imgPath);
console.log(returndata);
}
});
});
I'd like to pass a PHP session variable (called 'profileid') using FormData and AJAX. I thought this below would work, it did not. Why?
var imageData = new FormData();
imageData.append('image', $('#uploadImage')[0].files[0]);
imageData.append('profileid', <?php echo $_SESSION['profileid'];?>);
//Make ajax call here:
$.ajax({
url: '/upload-image-results-ajax.php',
type: 'POST',
processData: false, // important
contentType: false, // important
data: imageData,
//leaving out the rest as it doesn't pertain
You could add the profileid in the $.ajax URL parameter instead of adding it in FormData:
$(document).ready(function (e) {
$('#uploadImageForm').on('submit',(function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
url: "/upload-image-results-ajax.php?profileid=<?= $_SESSION['profileid']; ?>",
type: "POST",
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(response){
console.log("success");
console.log(response);
},
error: function(response){
console.log("error");
console.log(response);
}
});
}));
$('#uploadImage').on("change", function() {
$("#uploadImageForm").submit();
});
});
Don't forget to place session_start(); at the beginning of your code.