arun nair
arun nair

Reputation: 3683

why is url getting appended upon a ajax request using jquery form submission?

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.

Upvotes: 1

Views: 1716

Answers (1)

Patrick at CloudBudgie
Patrick at CloudBudgie

Reputation: 785

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.

Upvotes: 2

Related Questions