Join us at IRC!
The important thing is not to stop questioning. - Albert Einstein
Wednesday, May 23, 2012
Navigation
Members Online
Total Online: 37
Web Spiders: 16
Guests Online: 34
Members Online: 3

Registered Members: 70160
Newest Member: Furtif
Latest Articles
View Thread

HellBound Hackers | Computer General | Webmasters Lounge

Author

AJAX, JQuery and PHP.

NotMyFault
Member

Posts: 68
Location:
Joined: 23.12.09
Rank:
Hacker Level 3
Posted on 18-07-10 10:24
I'm new to JQuery and AJAX. I've only been learning them for ~1 day. I'm trying to make a form where the user puts in the data, hits submit and ajax takes the data and ships it off to my PHP scblockedript. The PHP scblockedript writes the data to two separate files and then returns 'OK' on success or the error on failure. This should then be printed out accordingly and the form hidden. The input is being written to the file ok but it's everything after that that's not working.

Here's the AJAX/JQuery scblockedript:

<html>
<head>
<title>JQuery trials</title>
<scblockedript type="text/javascblockedript" src="http://notmyfault.comxa.com/trial/ajaxForm/jquery-1.2.6.min.js"></scblockedript>
</head>

<body>
<scblockedript type="text/javascblockedript">
$ (document).ready(function()
{
$("p.toggle").click(function()
{
$("p.intro").toggle("slow");
});
});

</scblockedript>

<p class="intro" style="display:none;">Here's how it works: I have a couple of songs in my music library. I want to get more into music so if you have just heard a song or you want to tell me to listen to one, just add it here!
</p>
<p class="toggle" style="text-align:center; border:1px solid #BBBBBB;">Hide/Show</p>

<p>Here's my music library right now!</p>
<div class="library">

<div class="artists" style="float:left; width:100px; margin-bottom:20px;">
<?php include("artists.txt"); ?>
</div>
<div class="songs" style="float:left; width:100px; margin-bottom:20px;">
<?php include("songs.txt"); ?>
</div>

</div>



<scblockedript type="text/javascblockedript">
// we will add our javascblockedript code here

$(document).ready(function(){
var result=' ';

$("#ajax-form").submit(function(){

// 'this' refers to the current submitted form
var str = $(this).serialize();

$.ajax({
type: "POST",
url:"http://notmyfault.comxa.com/blog/widgetscblockedript.php",
data: str,
success: function(msg){

$("#note").ajaxComplete(function(event, request, settings){

if(msg == "OK") // Message Sent? Show the 'Thank You' message and hide the form
{
$ ("#fields").hide();
$ ("#note").html('Your song has been added. Thank you!');
}
else
{
$ ("#ajax-form").html(msg);
}


});

}

});

return false;

});

});

</scblockedript>
<br/><br/>
<div id="note" syle="clear:left;"></div>

<div id="fields" style="clear:left;">
<form id="ajax-form" method="POST" action="javascblockedript:alert('Success?');">
Artist: <input type="text" name="artist" />
<br />
<br />
Song: <input type="text" name="song" />
<br />
<br />
<input type="submit" name="submit" value="Add Song" />
</form>

</div>

</body>
</html>



And here's the PHP scblockedript:

<?php

$post = (!empty($_POST)) ? true : false;

if($post)
{

$artist = stripslashes( htmlentities($_POST['artist']) );
$song = stripslashes( htmlentities($_POST['song'] ) );

$error = '';

// Check artist

if(!$artist)
{
$error .= 'Please enter an artist.';
}

if(!$song)
{
$error .= 'Please enter a song.';
}

if(!$error)
{
$artistFile='artists.txt';
$songFile='songs.txt';

$artistFileHandle=fopen($artistFile, 'a') or die($error.='Server-Side Error');
$songFileHandle=fopen($songFile, 'a') or die($error.='Server-Side Error');

fwrite($artistFileHandle, "$artist\n") or die($error.='Server-Side Error');
fwrite($songFileHandle, "$song\n") or die($error.='Server-Side Error');

fclose($artistFileHandle) or die($error.='Server-Side Error');
fclose($songFileHandle) or die($error.='Server-Side Error');


if(!error)
{
echo "OK";
}

}//end of file write loop

else
{
echo $error;
}

}
?>


Thanks


EDIT-------------------------
Also, I got most of the jquery/ajax scblockedript from http://www.bitrepository.com/a-simple-ajax-contact-form-with-php-validation.html I took the idea for the php scblockedript from there too...
/EDIT------------------------




Edited by NotMyFault on 18-07-10 11:03
Author

RE: AJAX, JQuery and PHP.

only_samurai
[IRC Rockstar]

Posts: 984
Location: idling in some random irc channel
Joined: 18.08.06
Rank:
.|unranked|.
Posted on 18-07-10 21:52
I haven't run this on my own server to test it, but one thing that jumps out at me is the die($error.="texthere";); statements. AFAIK, die ends php execution, so the echo $error you've got at the bottom won't be run. You may be getting some sort of error after the files were written that isn't being written out and is preventing the echo "Ok" from being reached.


The problem with a fool-proof system, is eliminating the fool.

"His name is Cereal Killer...Like Fruitloops."
If you cut me, I bleed binary.

http://blog.psych0tik.net/
http://blog.psych0tik.net
Author

RE: AJAX, JQuery and PHP.

j4m32
Member

Posts: 81
Location:
Joined: 01.05.10
Rank:
God
Posted on 24-07-10 21:26
I think you need to change those lines, as suggested in the previous post
for example:
expresblockedsion: $artistFileHandle=fopen($artistFile, 'a')

But you also might need to change echo $error; for die($error); or add an exit; after the echo statement.


if(!(expresblockedsion)){
$error .= 'string here';
}
...
//Do this for all the expresblockedsions

}else{

die($error);

}


Hope that helps...
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.