I have used a jquery function to process a form which is given below.
$("#advertForm").submit(function(e)
{
var url=$(target).attr('title');
var imgname=$(target).attr('name');
var Form = { };
$.post('processAdvert.php', Form, function(data)
{
if(data == "Success")
{
setTimeout( function() { location=url }, 2500 );
return;
}
});
return false;
});
I need to write url and imgname to a text file. I know there is no way to write a file in jquery. Can I use any PHP function (fwrite) to write this variable into a file? How can I write this variable into a file?
Yes you can do that in some way. Send an ajax request to your php file. process your data, create a file, write some data using fput() method, Use force download class that will allow you to download the file at that moment. Ask if you have any confusion.
Related
I have a problem with jQuery. I'm checking the password and username with the code above.
It's working but the issue here is how can I send the data from this HTML form to my PHP file, or how can I start a session here and transfer it to PHP?
$("#girisButon").click(function (giris) {
if ($("#kadi").val() == "" || $("#sifre").val() == "")
alert('Username or Password is empty');
else
$.post($("#login").attr("action"),
$("#login :input").serializeArray(),
function (data) {
if (data == "1") {
$("#mesaj").removeClass("mesaj").addClass("basarili").text("OK").fadeIn(300);
// window.location.replace("index.php");
} else {
$("#mesaj").removeClass("basarili").addClass("mesaj").text("WRONG").fadeIn(300);
$("#giris").effect("shake", {
times: 2
}, 300);
}
});
$("#login").submit(function () {
return false;
});
});
$("#login").submit(function () {
window.location = 'phpprocessingpage.php';
});
Then, in phpprocessingpage.php, use POST variables to access any <input> elements that were part of the #login form:
$_POST['input1']
I like to save POST variables to my own local variables in this manner:
$input1 = (isset($_POST['$input1'])) ? $_POST['$input1'] : '';
You can't start a session on javascript and transfer it to PHP. You need to call PHP file on background with necessary parameters and start the session.
According to your script, you are currently making a request to your PHP file using $.post method, which takes URL from #login's action attribute.
Simply edit the PHP file and create sessions. After that, echo 1 to browser so javascript can understand your response.
Finally, this is the block that runs if server returns 1
if (data == "1") { }
Do whatever you want here. Sessions should be created.
I need to be able to replace a php file with another php file based on screen resolution. This is what I have so far:
<script type="text/javascript">
function adjustStyle(width) {
width = parseInt(width);
if (width = 1920) {
$('book.php').replaceWith('book2.php');
}
}
$(function() {
adjustStyle($(this).width());
$(window).resize(function() {
adjustStyle($(this).width());
});
});
</script>
which obviously isn't working-- any ideas? Thank you in advance for any help received.
UPDATE
Is this at all close (to replace the book.php line)?
{ $("a[href*='book.php']").replaceWith('href', 'book2.php'); };
Second Update to reflect input gathered here
function adjustStyle(width) {
width = parseInt(width);
if (width == 1920) {
$('#bookinfo').replaceWith(['book2.php']);
$.ajax({
url: "book2.php",
}).success(function ( data ) {
$('#bookinfo').replaceWith(data);
});
$(function() {
adjustStyle($(this).width());
$(window).resize(function() {
adjustStyle($(this).width());
});
});
}
}
I have not seen the use of replaceWith in the context you put it in. Interpreting that you want to exchange the content, you may want to do so my using the load() function of jQuery.
if(width == 1920){
$("#myDiv").load("book1.php");
} else {
$("#myDiv").load("book2.php");
}
Clicking on the button replaces the content of the div to book2.php.
The first problem is I don't think that you are using the correct selectors. If you have the following container:
<div id="bookContainer">Contents of book1.php</div>
The code to replace the contents of that container should be
$('#bookContainer').replaceWith([contents of book2.php]);
In order to get [contents of book2.php] you will need to pull it in by ajax using the following code I have also included the line above so that the data from book2.php will be placed into the container:
$.ajax({
url: "http://yoururl.com/book2.php",
}).success(function ( data ) {
$('#bookContainer').replaceWith(data);
});.
I haven't tested this so there might be an issue but this is the concept you need to accomplish this.
First off... using a conditional with a single = (equal sign) will cause the condition to always be true while setting the value of variable your checking to the value your checking against.
Your condition should look like the following...
if (width == 1920) { // do something }
Second, please refer to the jQuery documentation for how to replace the entire tag with a jquery object using replaceWith()... http://api.jquery.com/replaceWith/
I would use a shorthand POST with http://api.jquery.com/jQuery.post/ since you don't have the object loaded yet...
In short, my code would look like the following using $.post instead of $.ajax assuming I had a tag with the id of "book" that originally has the contents of book.php and I wanted to replace it with the contents of book2.php...
HTML
<div id="book">*Contents of book.php*</div>
jQuery
function onResize(width) {
if (parseInt(width) >= 1920) {
$.post('book2.php',function(html){
$('#book').html(html).width(width);
});
}
else {
$.post('book.php',function(html){
$('#book').html(html).width(width);
});
}
}
Hope this helps.
I'm trying to implement HTML5 drag and drop to upload a file. When the file is dropped, I want to call the php file to handle the dropped file. How can I call the php file and access the dragged file in php file. Also, I want to send the success or error message back from php file.
I'm unable to figure out how can I post the file to php and get the response from there.
My code so far is:
function drop(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files;
handleFiles(files);
}
function handleFiles(files) {
var file = files[0];
var reader = new FileReader();
reader.onload = uploadFile; //main function
reader.onloadend = uploadComplete;
reader.readAsDataURL(file);
}
function uploadFile(evt)
{
//call upload.php
//get success msg or error msg
//alert(success) or alert(error)
}
Here's example upload.php file:
<?php
$dropped_file //how to get the file
if (filesize($dropped_file) > 1024)
{
echo "size error" //how to send this error
}
else
{
echo "success" //how to send this success msg.
}
?>
This should help - http://www.thebuzzmedia.com/html5-drag-and-drop-and-file-api-tutorial/
You can use jQuery, upon the drop callback perform an AJAX call.
$("body").droppable({
accept: "img", //Your element type goes here e.g. img
drop: function(event, ui){
//Perform an AJAX call here. You can access the current dropped item through
//ui.draggable
}
)}
Use jQuery UI will give you the ability to drag and drop in the most easy way
Is it possible to have a flash/html5 video on a webpage, and when the video is done, it will run a PHP script?
You'd have to do a check inside both the html5 player and the flash player to determine if the video has stopped playing and then you should be able to call a php script through several ways. Let's say you have a php file called 'test.php' then in html5 you'd do the following:
<script>
var video = document.getElementsByTagName('video')[0];
video.onended = function(e) {
$.post(
"test.php",
function(data) {
/*Do stuff here!*/
},
"json"
);
}
</script>
In flash it's a bit different and you could try doing something like the following in actionscript3:
stream.addEventListener(NetStatusEvent.NET_STATUS, statusChanged);
function statusChanged(stats:NetStatusEvent) {
if (stats.info.code == 'NetStream.Play.Stop') {
// create a new loadvars variable
var lv:LoadVars = new LoadVars();
lv.load("http://www.myurl.com/test.php");
// now define what you want to do with the loaded data:
lv.onLoad = function(){
/*Do stuff here!*/
};
}
}
Lets say I have a file called functions.php, and it has two separate functions inside:
One would get the time
And the other would get the date
How will I, using JQuery AJAX retrieve data from the function that retrieves the date. How do I specify in the JQuery code which function on the server to pick.
I hope I am making sense. Thanks.
You could include a selector in the ajax request data. For example:
$.ajax({
url: "functions.php",
data: "function=time", // or function=date if you want date
...
});
Then in your PHP code, a simple if-statement will check which one to output.
if(isset($_GET['function'])) {
if($_GET['function'] == 'time') {
// do time stuff
} elseif($_GET['function'] == 'date') {
// do date stuff
}
}
You don't specify in jQuery what function to execute in PHP. What you do is request a document, possibly with a query string, and read the results. Your functions.php script is responsible for executing whatever is requested.
So, you might from jQuery request functions.php?fn=time in one place and functions.php?fn=date in another. Then, in functions.php, you would examine the query string and execute the appropriate function for what was requested, returning the results to jQuery.
$(document).ready(function()
{
$(".link").click(function()
{
var data['func'] = "time";
var url = functions.php
$.get(url, data, function(result)
{
$("#feedback").html(result);
});
});
});
then your php file would be,
if(isset($_GET['func']))
{
if($_GET['func'] == "time")
{
showTime();
}
else
{
showDate();
}
}
function showTime()
{
//show time
}
function showDate()
{
//show date
}
Code is untested but should be a good starting point for you.
you can add a parameter to the url:
example:
function.php?g=1
now, on the serverside check for the get parameter:
if($_GET['g']==1)
{
echo date();
}
else
{
echo time();
}
What is the response your are getting. You are getting the response in XML or some other format. If your response is XML try with this option.
$.ajax({
url:path,
data:{projectId:inpprojectId},
dataType:"xml",
success:function(data){
$(data).find("CheckAmount").each(function(){
total = $(this).find("TotalAmount").text();
usdAmt = $(this).find("PettyCashAmount").text();
validateBudget(total,inp);
});
}
});