Jump to content
  • Advertisement
Sign in to follow this  
soitsthateasy

[web] AJAX, JQuery and PHP Problem.

This topic is 2954 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 script. The PHP script 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 script:


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

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

</script>

<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>
&lt;p class="toggle" style="text-align:center; border:1px solid #BBBBBB;"&gt;Hide/Show</p>

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

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

</div>



&lt;script type="text/javascript"&gt;
// we will add our javascript 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;

});

});

&lt;/script&gt;
&lt;br/&gt;&lt;br/&gt;
&lt;div id="note" syle="clear:left;"&gt;</div>

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

</div>

&lt;/body&gt;
&lt;/html&gt;




And here's the PHP script:


&lt;?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;
}

}
?&gt;



Thanks

Share this post


Link to post
Share on other sites
Advertisement
I just glanced over your code, but this caught my eye:

$error = '; <-- This is going to screw up your code (should ='';). I'm surprised it doesn't trigger an error somewhere, actually, which makes me think you might have another odd number of 's on another line I didn't notice... Or it's a typo in the post.

Your JS looks fine (though I haven't used jQuery in a while), what other part isn't working?

...Nevermind, took a look at your script (you left the URLs in the code). Your host automatically appends code* that's messing up your script. Try taking only the first line (substr from 0 to \n) and then running your response checks.

*Actual response:
OK

<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>
<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></noscript>

Share this post


Link to post
Share on other sites
Just a note about quotes: I've noticed that this board mangles two adjacent single quotes in code listings, turning them into one quote character.
Two single quotes: '';
Two single quotes with a space: ' ';

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!