I'm struggeling with making an AJAX url inside of an php echo. Other troubleshooters around the internet didn't get me through it. Here is my code:
<script type="text/javascript">
$(document).ready(function () {
$.ajaxSetup ({
cache: false
});
var ajax_load = "";
$( "#'.$row_items['id_aanbod'].'" ).change(function() {
$("#res").html(ajax_load).load("update.php", "e=" + $("#'.$row_items['id_aanbod'].'").val() & "id=" + $("#hrr").attr("id"));
});
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
The strange thing is that if I use only one variable the script works like it should, but as soon as I insert the second part there is nothing happening. Can anyone help me with this?
& "id="
should be
+ "&id="
But for clean code, you could write like below:
$("#<?php echo $row_items['id_aanbod']?>").change(function() {
$("#res").html(ajax_load).load("update.php", {
e: this.value,
id: $("#hrr").attr("id")
});
});
Please try like this:
<script type="text/javascript">
$(document).ready(function () {
$.ajaxSetup ({
cache: false
});
var ajax_load = "";
$( "#<?=$row_items['id_aanbod']?>" ).change(function() {
$("#res").html(ajax_load).load("update.php", "e="+$("#<?=$row_items['id_aanbod']?>").val() + "&id=" + $("#hrr").attr("id"));
});
});
Don't forget to check vals. If short open tags is OFF -> then try :
<?php echo $row_items['id_aanbod']?>
Related
I used ajax to send to me a php variables in this way:
<script type="text/javascript">
$(document).ready(function(){
$("#disdiciButton").click(function(){
var log_user = <?php echo json_encode($log_user);?>;
alert(log_user);
$.ajax({
type: 'POST',
url: 'disdici.php',
data:log_user,
success: function(data) {
$("#deleteResponse").text(data);
}
});
});
});
</script>
Now I'm trying to collect this data in the php page disdici.php but I don't know how. I've tried in this way:
$log_user = "";
if(isset($_POST['data'])){
$log_user = json_decode(data);
}
echo 'user: '.$log_user;
but $log_user remain empty. How can I do?
You have to write your data like :
data : 'var1=' + value1 + '&var2=' + value2 + '&var3=' + value3...
So, you can get the variable in php :
$_POST['var1'] = value1;
...
In your example :
<script type="text/javascript">
$(document).ready(function(){
$("#disdiciButton").click(function(){
var log_user = <?php echo json_encode($log_user);?>;
alert(log_user);
$.ajax({
type: 'POST',
url: 'disdici.php',
data:'log_user=' + log_user,
success: function(data) {
$("#deleteResponse").text(data);
}
});
});
});
</script>
In PHP file, you get the value :
$_POST['log_user']
I'm trying to do something like user click on button, it sends request to json, and i want it shows the response of json, but i have some errors that i can't figure it out how to solve it.
HTML:
<a class="backLink" id="target_button" onclick="insertText('target_', 'AA', 'NN')"><div class="fa fa-external-link"></div>Show</a>
JS + ajax:
<script type="text/javascript">
function insertText(elemID, text, text2) {
$.ajax({
url: "https://myserver.com/jsons.php?request=getTargets&tar="text"&bar="text2"",
beforeSend: function (xhr) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
}
})
.done(function (data) {
if (console && console.log) {
console.log(data);
var getValue = $("#" + elemID).val() == "" ? data : "";
$("#" + elemID).val(getValue);
}
});
}
</script>
I'm not sure if im doing it in the better way.
It is something simple. User click on button, call the function with that parameters, make a request to json , receive answer and display it on the elemID.
Thanks
EDIT:
changes i made
<script>
console.log('called');
function insertText(elemID, product, country) {
"use strict";
console.log('called2');
$.ajax({
url: 'https://myserver.com/jsons.php?request=Targ&country=" + country + "&product=" + product +".",
beforeSend: function (xhr) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
}
})
.done(function (data) {
if (console && console.log) {
console.log(data);
}
});
console.log('called3');
}
console.log('called4');
</script>
result on console:
called
called4
it looks like i can't enter on function
Write console.log('called'); in your insertText() function right before the line contains $.ajax({
So you can see the function is called on click or not.
If the Console tab of the Developer Tools throws JavaScript errors, this could brake further JavaScript execution too.
Replace your double quotes with single quote.
URL should be
url: 'https://myserver.com/jsons.php?request=getTargets&tar='+ text +'&bar='+ text2
How I did it:
<script type="text/javascript">
function insertText(elemID, p, c) {
var server = window.location.hostname;
$.ajax({
url: "https://" + server + "/jsons.php?request=Target&c=" + c + "&p=" + p +"",
beforeSend: function (xhr) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
}
})
.done(function (data) {
console.log(data)
var tmp = data.replace('"',"");
var tmp2 = tmp.trim();
var getValue = $("#" + elemID).val() == "" ? tmp2 : "";
$("#" + elemID).val(getValue);
});
}
</script>
HTML:
<a id="t_button" onclick="insertText('t_d', '<?php $c ?>', '<?php $p ?>');" > show</a>
Hope it helps someone another !
I ask you for help. Namely, struggling with the tooltip in ajax. Everything works beautifully when the page is load or after such as F5. However, in the part web I use refresh div every 60 seconds by ajax
<script type="text/javascript" >
$.ajaxSetup({ cache: false });
var auto_refresh = setInterval(
function()
{
$('#loaddiv').load('refresh_clusterdx_2.php');
}, 60000);
</script>
The code of my tooltip
<script type="text/javascript">
$(document).ready(function(){
function showProfileTooltip(e, id){
var top = e.clientY -45;
var left = e.clientX + 25;
$('.p-tooltip').css({
'top':top,
'left':left
}).show();
//send id & get info from get_prefix.php
$.ajax({
url: '/Info/get_prefix.php?id='+id,
beforeSend: function(){
$('.p-tooltip').html('Loading..');
},
success: function(html){
$('.p-tooltip').html(html);
}
});
}
function hideProfileTooltip(){
$('.p-tooltip').hide();
}
$('.profile').mouseover(function(e){
var id = $(this).attr('data-id');
showProfileTooltip(e, id);
});
$('.p-tooltip').mouseleave(function(){
hideProfileTooltip();
});
});
</script>
All beautifully and looks ok until the div is not refreshed. When a div to be refreshed, the tooltip no work :( I can not find a solution to the problem, whether it is at all possible to solve.
Thank you for any help.
Regards
tjakob
To ensure that your functions work after ajax loaded content, you'll have to modify them a little:
$(document).on('mouseover', '.profile', function() {
var id = $('.profile').attr('data-id');
showProfileTooltip(e, id);
});
$(document).on('mouseleave', '.p-tooltip', function() {
hideProfileTooltip();
});
You should always use .on with dynamically loaded content - I'm in the habit of doing this for all my functions now.
I want to make something simple but the return of AJAX is coming on all page not to the DIV I want to.
Here is my code PHP: file_ajax.php
<?php
require_once "../../funct/cfg.php";
if(isset($_POST['id'])){
if(fileDB($_POST['id'])){
echo '<script type="text/javascript">
var albhost="albup.ex";
var albbg="111111";
var albvi="2013";
var albid="'.$_POST['id'].'";
var albw=326;
var alblight="D69E9E";
var albfront="C4C4C4";
var albvol=80;
var albas="true";
var albdownb="0";
</script>
<script type="text/javascript" src="http://albup.ex/api/explayer/embed.js">
</script>';
exit;
}
}
?>
and the file ajax: ajax.js
function PrewThis(id,style){
var dataString = 'id=' + id + '&style=' + style;
$("#fileprev").removeClass();
$("#fileprev").show().addClass(style);
$.ajax({
type: "POST",
url: "ajax/file_ajax.php",
data: dataString,
context: "#fileprev",
error: function(){ $("#sidebox").append("SOMETHING ERROR") },
success: function(html){
$("#fileprev").append(html);
}
});
}
And the call is:
<a href="#"
onclick="PrewThis('MRuw2ZbMXj','oranger'); return false"
class="oranger">Sean Paul - Got 2 Luv U (Feat. Alexis Jordan)</a>
Please someone help ?
It's just a guess, but I think this line is the source of your problem...
<script type="text/javascript" src="http://albup.ex/api/explayer/embed.js">
As it states in the jQuery docs: "If html is specified, any embedded JavaScript inside the retrieved data is executed before the HTML is returned as a string."
It seems possible that the included script's output is blowing up your page.
Nothing else in your code seems to indicate a problem. It all looks good otherwise, but then I haven't seen your HTML source.
I'm looking to auto submit when a specific checkbox is checked.
I need it to pass to ajax.
Here is what I have so far:
$(document).ready(function () {
$("input[name=<?php echo("$newseo"); ?>]").click(function(){
var id=$(this).attr('id');
var favorite=$(this).val();
$.ajax({
type:'POST',
url:'check_favorite.php',
data:'id= ' + id + '&favorite='+favorite
});
}
});
});
But I just can't seem to get it to work,
Any help would be great, Thanks!
Here you go, this should do it. Your AJAX looks fine. I put together a JSFiddle to demonstrate.
$("input[name=TestCheck]:checked").live('click', function(e) {
var id=$(this).attr('id');
var favorite=$(this).val();
alert(id + " - " + favorite);
// Post here ...
$.ajax({
type:'POST',
url:'check_favorite.php',
data: {id: id, favorite: favorite}
});
});
JSFiddle : http://jsfiddle.net/4GQ6K/1/
I don't really like using obtrusive JavaScript and inputting PHP into JavaScript like that but there is no reason it shouldn't work.
$('#my_checkbox').change(function(){
if($(this).is(':checked'))
{
$('#my_form').submit();
}
});
Use your server side code[php] to set a id or a class to your specific checkbox. Then bind a click event to the given class name, e.g.
php code sets a class named .sCheckBx.
then on document.ready bind your event :
$(document).ready(function () {
$(".sCheckBx").click(function(){
var id=$(this).attr('id');
var favorite=$(this).val();
$.ajax({
type:'POST',
url:'check_favorite.php',
data:'id= ' + id + '&favorite='+favorite
});
}
});
});
Try this
$(document).ready(function () {
$("checkboxId").click(function(){
var $this = $(this);
if($this.is(":checked")){
var id=$this.attr('id');
var favorite=$this.val();
$.ajax({
type:'POST',
url:'check_favorite.php',
data:'id= ' + id + '&favorite='+favorite
});
}
}
});
});