Hi,
i have two scripts,
One holds the form and the other the php Query process.
Im using a small piece of javascript to POST the value to my php Query process script
but im finding it difficult to display a result by fading in a div based on the query response.
For example if i type in a tag and the tag already exists i want the php to check the table and the return a fadeIN div to say it "already exists" otherwise fadeIN a div to say "Tag inserted into database"
But i also am using a little more JS to clear the input field value as well.
All this is done without reloading the page.
So basically if the tag "People" exists in the MYSQL table then i want the div #i2 to fade in and say "Already exists!"
My code is below:
The HTML
<form method="post">
<div>
<strong>Keyword: </strong> <input type="input" id="tag" name="tag"/> <input type="button" id="send" value="Add" /> </div>
<div id="i1"><br>
<p><strong>Success:</strong> Add another keyword and press Add.</p>
</div>
<div id="i2"><br>
<p><strong>Failed:</strong> The keyword already exists.</p>
</div>
</form>
The Javascript
<script type="text/javascript">
$('#i1').hide();
$('#i2').hide();
$("#send").click(function() {
var txt = $.ajax({
url: 'process.php',
async: true, // or false, depends on your needs
type:'POST',
data:({
tag:$('input#tag').val(),
})
}).success;
//processed in the php file??
});
</script>
and this is the PHP processing page
process.php
<?php
$tagname = mysql_real_escape_string($_POST['tag']);
$sqll = "SELECT COUNT(tag_name) FROM Tags WHERE tag_name = '$tagname'";
$result = mysql_query($sqll) or die(mysql_error());
$status = mysql_fetch_row($result);
if($status[0] > 0) { ?>
<script type="text/javascript">
// Not successfull div
//For IE - this removes the value in the input field
$("#tag").replaceWith($("#tag").clone(true));
//For other browsers - this removes the value in the input field
$("#tag").val("");
// this should fadein a div called i2 and then fade it out
$('#i2').show('slow');
setTimeout(function(){
$('#i2').fadeOut('slow'); }, 3000);
</script>
<?php } else { ?>
// This is where i would process my INSERT query and also run the javascript below to fade in my success DIV
<script type="text/javascript">
// Successful div
//For IE - this removes the value in the input field
$("#tag").replaceWith($("#tag").clone(true));
//For other browsers - this removes the value in the input field
$("#tag").val("");
// this should fadein a div called i1 and then fade it out
$('#i1').show('slow');
setTimeout(function(){
$('#i1').fadeOut('slow'); }, 3000);
</script>
<?php }} ?>
I hope the above makes sense.
Thank you in advance :-)
John