I have set up links that when clicked I want it to copy the text. It outputs many links so I have to make sure zClip copies the right one:
<a class="copy-callbacks" id="coupon-code-copy-<?php echo $couponid ; ?>" href="#"><?php echo $info['coupon'] ; ?></a>
This is the jQuery I am using but it is not working:
$(document).ready(function(){
$("a.copy-callbacks").zclip({
path:'scripts/ZeroClipboard.swf',
copy: $('#' + myCopyID).text(),
beforeCopy:function(){
var myCopyID = $(this).attr("id");
},
afterCopy:function(){
}
});
});
Using this code the flash object doesn't even load. I'm not quite sure what is going wrong. I've tried using:
copy: $(this).text(),
The flash object loads, but for some weird reason it copies some text in the footer. Any help is greatly appreciated!!
I've never had to use the .attr("id"), I always do the following.
<span class="swfTitle" style="position: relative; onmouseover="this.style.cursor='pointer'">
Text
</span>
$('.swfTitle').zclip({
path: '/js/ZeroClipboard.swf',
copy: function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
async: false,
data: JSON.stringify(PageInfo),
url: "dosomethinghere.aspx&query=string",
timeout: 30000,
success: function (data) {
PageInfo = JSON.parse(data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
return PageInfo.PostHTML;
}
Where PageInfo is a js object. Overall, id tags are different than the class tags, .swfTitle is different than #swfTitle.
Related
I have this code located on the server in a php file
<?php
include "db.php";
$data=array();
$idFacebook=$_POST['idFacebook'];
$q=mysqli_query($con,"select `gender` from `users` where
`idFacebook`='$idFacebook'");
while ($row=mysqli_fetch_object($q)){
$data[]=$row;
}
echo json_encode($data);
?>
I have this code on my website
$(document).ready(function() {
var url = "http://xxxxxxxxx.com/calGetBackupJSON.php";
$.getJSON(url, {idFacebook: 11111111111}, function(result) {
console.log(result);
});
});
Currently, I can manually change the idFacebook on the server and it returns the correct info. BUT
How do I pass the idFacebook from the web page to server?
So it only returns one specific user's gender?
Thank you very much
Add a HTML input Eg <input type="text" id="idFacebook" /> and a button <button id="submitIDFB"></button> in your web page.
Change your JS content:
$('#submitIDFB').click(function(e) {
e.preventDefault();
var idFacebook = $('#idFacebook').val();
var url = "http://xxxxxxxxx.com/calGetBackupJSON.php";
$.getJSON(url, {idFacebook: idFacebook}, function(result) {
console.log(result);
});
});
You may do some data validation / data preparation first. :D
UPDATED:
If you want to handle error, use AJAX:
$.ajax({
dataType: "json",
url: url,
data: {idFacebook: idFacebook},
success: function(result) {
console.log(result);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
So, I load partial views inside the "main" view, and put the content inside a div, like this:
<div class="page-content" id="inside_page"></div>
<script>
$("#myTab").click(function() {
location.hash="myTab";
App.blockUI({
boxed: !0
});
$.ajax({
type: 'POST',
url: "<?php echo base_url();?>"+"index.php/welcome/myTabContent",
data: {},
dataType: "html",
success: function(data) {
if (data.status == 2) {
window.location = "/";
}else{
window.scrollTo(0, 0);
App.unblockUI();
$("#inside_page").empty();
$("#inside_page").html(data);
}
},
error: function (xhr, status, errorThrown) {
App.unblockUI();
bootbox.alert("<p>Error</p>");
}
});
});
</script>
The partial view includes tags, and when I change tabs, or load the tab again, scripts that have been already loaded, will not be removed and will keep running until the user reloads the page. How can I stop that from happening?
I have been searching through all the answers here but i can't get it to work.
I want to send two integer values from ajax to php.
Here is the ajax part:
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var id_user=$(this).filter(':checked').val();
var stringname=$(this).attr('name');
var substr = stringname.split('_');
var id_paper=substr[1];
alert('User_id is: '+id_user +'. And paper_id is: '+id_paper);
$.ajax({
type: "POST",
url: "ajax/expositor.php",
data: {ID_ponencia: id_paper, ID_Participante: id_user},
//contentType: "application/json; charset=utf-8",//type of response
//contentType: "application/x-www-form-urlencoded;charset=utf-8",
//dataType: "json",
beforeSend:function(){
alert('This is AJAX. The following is about to be sent. paper:'+id_paper+' user: '+ id_user);
},
success:function(response){
var msj='#msj_'+id_paper;
$(msj).append(response);
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});
});
});
I have tried with the commented parts of dataType and contentType and it doesn't work either.
Here is the php code.
if(isset($_POST["ID_ponencia"]) && !empty($_POST['ID_ponencia']) && isset($_POST['ID_participante']) && !empty($_POST['ID_ponencia']))
{
$ID_participante=$_POST['ID_participante'];
$ID_ponencia=$_POST['ID_ponencia'];
echo ('<p style="color: green;">Success! ☺</p>');
}else{
//Output error
echo ('<p style="color: red;">error!</p>');
header('HTTP/1.1 500 There has been an error!');
exit();
}
The php is working fine, since i have tested an html form posting to that php script and i get the successfull message. However, from the ajax script i get the 500 error.
Any help will be really appreciated.
Your ajax is setting param of ID_Participante (capital P), but php is checking for ID_participante (lowercase p).
Also, you're checking if ID_ponencia is not empty twice...think you meant for the second one to be checking ID_participante.
I have a have a button that calls a JavaScript method that looks like this:
processSelection = function(filename) {
//alert('method reached');
$.ajax({
url: "sec/selectUsers.php",
data: "filename="+filename,
cache: false,
dataType:'html',
success: function(data) {
//$('#uploader').html(data);
$('#noUsers').sortOptions();
values = $('#noUsers').children();
for (i = 0; i < values.length; i++) {
$(values[i]).bind('dblclick',switchUser);
}
$('#addButton').bind('click',addSelection);
$('#removeButton').bind('click',removeSelection);
$('#submitButton').bind('click',addUsersToFile);
},
error: function (request, textStatus, errorThrown) {
alert('script error, please reload and retry');
}
}); /* ajax */
}
It is not going to the selectUsers.php script, nor is it posting the error message. When I click on my button 'add users' it does nothing. The other methods: switchUser, removeSelection, addSelection, and addUserstoFile are already defined.
I am fairly new to JavaScript and php and have been assigned this project running maintenance on our website. My php_error.log shows no error either. If anyone has any advice on this specific problem, or debugging in general I would very much appreciate it.
here is the click event:
<input type="button" value="add users" onclick="processSelection('<?=$drFile['name']?>')"/>
Okay,
To simplify my problem, I have done this:
processSelection = function(){
//alert('method reached');
$.ajax({
url: "sec/testPage.php",
cache: false,
success: function() {
alert('success');
},
dataType:'html',
error: function (request, textStatus, errorThrown) {
alert('script error, please reload and retry'); }
});
}
where testPage.php is just a table with some values in it.
Now when I click the button it show 'success', but never shows testPage.php
dataType: 'HTML'
has to be before your success method as far as i know. If still does not work try the following:
it will not show the test page because you are not appending anything to your current body. Your script executes successfully if you see "success"on your screen. All you need to do is if you have html generated in your testPage, assign all html to a php variable and then just echo it instead of returning it like
echo $myhtmlgenerated
and change
success: function() { ....
TO
success: function(result) {
$(body).append(result);
}
or you can play around with it and specify a special div which will hold the content from that page.
I have this method that I want to run a php file using ajax and then reload the page.
function winA()
{
var x = "<?php echo $id;?>"
$.ajax({ url: 'w.php5' ,
data: { id: x },
success: function(data) {
window.location.reload()
}
});
}
This is what I have and I've looked it over endless times for flaws, made sure the php variable is reading properly and made sure the function is truly being called. The php file works properly when called w.php5?id=1
Why won't this ajax call work?
Thanks in advance for the help, Aaron.
function winA()
{
var x = "<?php echo $id;?>"
$.ajax({ url: 'w.php5' ,
data: { id: x },
success: function(data) {
window.location.reload()
}
error:function (xhr, ajaxOptions, thrownError)
{
alert(xhr.status);
alert(thrownError);
}
});
}
This way it will show alert in case of ajax error
Also, if in chrome, press the combination Ctrl+Shift+I for developer tools and check network tab to see if w.php5 is called, and what is the response. Dont know tools for other browser but there should be something like that
There are 2 alternatives.
If you want to post some other data, use this
.ajax({
type: 'POST',
url:'w.php5',
data: {id: '<?php echo $id; ?>'},
success: function(resp){
console.log(resp);
},
dataType:'json'
});
If you go this way, your ID is going to be stored in $_POST array => *$_POST['id']*
If you want to just get some data by ID you post, use this
.ajax({
type: 'GET',
url:'w.php5?id=<?php echo $id; ?>',
success: function(resp){
console.log(resp);
},
dataType:'json'
});
If you go this way, your ID is going to be stored in $_GET array => *$_GET['id']*
You're missing a semicolon here:
var x = "<?php echo $id;?>"
Should be:
var x = "<?php echo $id;?>";
//set the method
POST or GET
type:'GET'; or type:"POST"
That url is probably missing a leading forward-slash, assuming you are trying to access a url like www.myurl.com/w.php?id=5
Try
url: '/w.php?id=5',
If that doesn't work, you need to inspect the request using a developing tool within Chrome or Firefox.
You can also var_dump the $_GET or $_POST in w.php, as the response will expose the output.