1

Stuck with a jquery ajax form submission! Here's the code snippet:

Javascript/ HTML Code:

<form action="">
<button type="submit" class="submit-button-minus" id="submit-minus" name="submit-minus"><span class="hide-text"></span></button>                
</form>

<form action="">
<button type="submit" class="submit-button-plus" id="submit-plus" name="submit-plus"><span class="hide-text"></span></button>
</form>

<!--store current page id from php variable into javascript variable-->
<?php echo "<script> jsPageID = " .$page_id. ";</script>"; ?>                   
<?php echo "<script> jsUsername = '" .$_SESSION['username']. "' ;</script>"; ?>     


<script type="text/javascript">
$('document').ready(function(){

jsPageID = parseInt(jsPageID); 
$('#submit-minus').click(function(){
jsPlusOrMinus = "minus";                    
ajax_painting_rating(jsPlusOrMinus, jsPageID, jsUsername);                  
});

$('#submit-plus').click(function(){
jsPlusOrMinus = "plus";
ajax_painting_rating(jsPlusOrMinus, jsPageID, jsUsername);
});

function ajax_painting_rating(jsPlusOrMinus, jsPageID, jsUsername)
{
$.get(
'ajax-painting-rating.php', 
{plus_or_minus: jsPlusOrMinus, page_id: jsPageID, user_name:jsUsername }, 
function(data){     
$('#rating-value').html(data);
}
);
}

});

</script>

Now here's the PHP code:

<?php
include("include_db_connection.php");

$get_plus_or_minus = $_GET['plus_or_minus'];
$get_page_id = $_GET['page_id'];
$get_user_name = $_GET['user_name'];

if($get_plus_or_minus == "plus")
{

    $rating=1;
    $rating_insert_query =  "INSERT INTO ..............";       

    $rating_insert_result = mysqli_query($db_conn, $rating_insert_query);
}

if($get_plus_or_minus=="minus")
{

    $rating=-1;
    $rating_insert_query =  "INSERT INTO ..............";       
    $rating_insert_result = mysqli_query($db_conn, $rating_insert_query);
}

/* more queries here....*/  
/*this is what i will be returning back from the server, to the ajax request*/

echo $FINAL_RESULT;

?>

Now, when I try executing the above code, the form "sometimes" gets submitted to db. I cant think of a reason for why the "sometimes".

Also, there is some data that is getting appended to the url somehow, after the request is processed. Example: If the original url of the webpage, before making the ajax request is something like "page.html?id=6" then, on executing the ajax request, the page url gets appended as below:

"page.php?id=6&submit-minus=" (if the submit-minus button was used for invoking the ajax request)

I do not understand why the submit-minus thing is getting appended to the url, and also whey only rarely the data is getting inserted successfully to the db.

Thanks for your time.

1 Answer 1

2

The query vars submit-minus and submit-plus are getting appended to the url because you are doing a GET, and GET will take any form variables on a submit, and append them to the URL.

I can't see enough of your database code to tell you why it sometimes works and sometimes does not, but you will only write to the database if $get_plus_or_minus is set. So, if your url says "submit-minus=" without any value, you would not write to the database.

Sign up to request clarification or add additional context in comments.

7 Comments

But isn't this supposed to be an ajax request? So would using GET append any data into the url? Also, I noticed that the page on the whole is getting loaded again, in which case I dont see the effect of ajax at all upon executing. Why is it so?
You are using an HTML button tag, with a type of submit, which makes it submit the form. If you change the type to button, it will not submit the form
ooooooopsiee..!!!! THAT SOLVED. To think that just changing the type of the submit button from "submit" to "button" could solve this whole thing! I spent like 2-3 hours fiddling with the code and using different form post methods and what not...!!! simply didnt bother to see this line! THANKS A LOT..!!!
Well, that's why this site exists :)
Just one minor issue - On clicking the plus/ minus buttons, the ajax request is handled correctly in firefox. Even if i keep clicking those buttons, the ajax code is run again and again as required by firefox. However, in IE, the code is run only once. After this, even on clicking the plus/minus buttons n number of times, the code doesnt run again. Is it some problem with IE?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.