Hey I can't pass a JS variable through AJAX to php file. No matter if I try POST or GET it's not working: with POST it gave me [] or Undefined index if I want to print_r($_POST['adminID']
And GET, there I get {"fancybox":"true"}
$('#adminList tr').on('click', function() {
var adminID = $(this).find(".adminID").text();
console.log(adminID);
$.ajax({
type: 'POST',
url: 'modules/management/user_edit.php',
data: {adminID : adminID},
success: function(data)
{
$.fancybox.open({
src : 'modules/management/user_edit.php',
type : 'ajax'
});
}
});
});
and module/management/user_edit.php i just want to post this adminID
You send double request to user_edit.php file, first with POST data, and second without. You should display result of first request in fancybox.
$('#adminList tr').on('click', function() {
var adminID = $(this).find(".adminID").text();
$.ajax({
type: 'POST',
url: 'modules/management/user_edit.php',
data: {adminID : adminID},
success: function(response)
{
$.fancybox.open(response);
}
});
});
Related
I'm trying to create a search field on the top navbar with ajax. Since it's on the navbar, it has to be present on all pages, therefore the URL is constantly changing.
$.ajax({
type: 'GET',
url: CURRENT_URL,
dataType: 'json',
data: {
search: userSearch
},
success: function (){...
I'm working with Laravel, so i tried this on the navbar page:
<script>
var CURRENT_URL = "{{url()->current()}}"
</script>
The CURRENT_URL is displayed fine if i try to console log it, but ajax gives an error "Source map error: Error: request failed with status 404".
How can i insert the current URL into the ajax request?
You can use location.href instead of CURRENT_URL
$.ajax({
type: 'GET',
url: location.href,
dataType: 'json',
data: {
search: userSearch
},
success: function (){...
or
<script>
var CURRENT_URL = location.href;
</script>
Hopefully, this can help. Here's my approach to make my url dynamically I get the URL in every forms var URL = $('#example_form').prop('action'); then the URL variable must append or set via Js/Jquery. check my example below.
<script type="text/javascript">
var URL = $('#example_form').prop('action');
$.ajax({
type:'GET',
url: URL+'/clearContent',
beforeSend: function (xhr) {
var TOKEN = $('meta[name="csrf-token"]').attr('content');
if (TOKEN) {
return xhr.setRequestHeader('X-CSRF-TOKEN', TOKEN);
}
},
data:{
get_category_id : $('.parent-id').val(),
},
success:function(data){
if (data.response.status == true) {
// your codes here
}
},
dataType: 'json',
complete: function () {}
});
I am trying to Execute Ajax in Yii.
But gives me error as :
Empty string passed to getElementById()
Below is my code of AJAX :
$url1 = Yii::$app->homeUrl;
$crurl = 'https://'.$_SERVER['SERVER_NAME'].$url1;
$this->registerJs('$(document).ready(function(){
$(".glyphicon.glyphicon-eye-closed").click(function(){
$.ajax({
url: "'.$crurl.'?r=history/move",
type: "post",
data: {id: this.id},
success: function(data){
alert(data);
}
});
});
});
Check the following options;
Check the html code that the .glyphicon.glyphicon-eye-closed classes has that has id attr?
Replace this.id to $(this).attr('id');
I am trying to pass a value of a button using some ajax to a separate file.
Here is my code.
$("input#downloadSingle").click(function() {
var myData = $("input#downloadSingle").val();
$.ajax({
type: 'post',
url:'singleDownload.php',
data: myData,
success: function(results) {
alert('works');
}
});
});
However, when I test out the next page by doing a var_dump on $_POST. I don't get any data back. Thoughts?
You're not specifying the name of the $_POST variable you're gonna get on the singleDownload.php file (unless that's part of the button's value), so you should try something like:
$("input#downloadSingle").click(function() {
var myData = "whatever=" + $("input#downloadSingle").val();
$.ajax({
type: 'post',
url:'singleDownload.php',
data: myData,
success: function(results) {
alert('works');
}
});
});
And make sure $_POST in your php file is the same whatever variable
Right now I've got a ui dialog on a page, and I need some way to pass a javascript variable to the php within the dialog via AJAX. Here's my code:
$('.user').click(function(){
var user = getID($(this).attr('id'),'User');
$.ajax({
type: "POST",
url: "test.php",
data: 'user=' + user,
success: function(){
$("#dialog").dialog('open');
}
});
});
and the beginning of the PHP:
<?php
if(isset($_POST['user'])){
echo '<center><b>User: '.ucfirst($_POST['user']).'</b></center><br />';
}
The problem is, it's just not getting passed. I'm very new with Ajax so I'm sure I'm messing something up.
This is not working because you don't specify the return variable, see your code with correction:
$('.user').click(function(){
var user = getID($(this).attr('id'),'User');
$.ajax({
type: "POST",
url: "test.php",
data: 'user=' + user,
success: function(data){ // Here you specify the callback variable from the AJAX call
alert(data); // Here will show '<center><b>User: UserExample </b></center><br />';
$("#dialog").dialog('open');
}
});
});
You can make your dialog content my loading some page like Userpage.php and then pass User as a url parameter
$('.user').click(function(){
var user = getID($(this).attr('id'),'User');
$.ajax({
type: "POST",
url: "test.php",
data: 'user=' + user,
success: function(){
$("#dialog").load('/userpage.php?User=' + user).dialog('open');
}
});
});
With this jquery :
$.ajax({
type: "POST",
url: "tosql.php",
data: {
content: content
},
success: function() {
}
});
I was wondering if I could receive a header(location: ) from the php, to redirect to another page with specific values stored in the url. Here is the url: header(location: 'morefive.php?document='.urlencode($id))
Assuming tosql.php returns id you could do this :
$.ajax({
type: "POST",
url: "tosql.php",
data: {
content: content
},
success: function(data){
window.location = 'morefive.php?id=' + data;
}
});
Update :
On tosql.php :
$_POST['content'];
// do something with content
echo $id; //say $id = 123
You would be directed to morefive.php?id=123 on success.