user2834795
user2834795

Reputation: 4503

No alert in success function

I am trying to insert value in database from jquery ajax and i want whenever data insertion is successfull, a result output comes true other wise "error:failed". My entry in database successfully updated, but when i alert(msg), its doesnt give me message.

          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">    </script>
  <body>
    <div class="wrapper">
    <div id="main" style="padding:50px 0 0 0;">
         <!-- Form -->
       <form id="contact-form"  method="post">
        <h3>Paypal Payment Details</h3>
        <div class="controls">
            <label>
                <span>TagId</span>
                <input placeholder="Please enter TagId" id="tagid" type="text" tabindex="1" >
            </label>
        </div>
        <div class="controls">
            <label>
                <span>Paypal Email: (required)</span>
                <input placeholder="All Payment will be collected in this email address" id="email" type="email" tabindex="2">
            </label>
        </div>
        <div class="controls">
            <label>
                <span>Amount</span>
                <input placeholder="Amount you would like to charged in GBP" id="amount" type="tel" tabindex="3">
            </label>
        </div>
                    <div class="controls">
                 <div id="error_div"></div>
                 </div>
        <div>
<button name="submit" type="submit" id="form-submit">Submit Detail</button>
        </div>
    </form>
    <!-- /Form -->

    </div>
</div>
 <script type="text/javascript">
        $(document).ready(function(){
   $('#form-submit').click(function()
    {
    var tagid = $("#tagid").val();
var email = $("#email").val();
var amount = $("#amount").val();
var param = 'tagid='+ tagid + '&email=' + email + '&amount=' + amount;
    param = param + '&type=assign_amount';
    locurl = 'dbentry.php';
          $.ajax({
              url: locurl,
              type:'post',
              data:param,
              success:function(msg)
                {  
                    alert(msg);            
                }
              });
});
  });

dbentry.php

  <?php
$vals = $_POST;
    include 'dbconfig.php';

    if($vals['type'] == "assign_amount")
{
    $values = assign_amount();  
    echo json_encode(array('status' =>$values));    
}


function assign_amount()
{
    global $con;
    global $vals;
    $sql = "INSERT INTO `dynamic_url`(`tagid`,`email`,`amount`) VALUES('".$vals['tagid']."','".$vals['email']."','".$vals['amount']."')";
    $result = mysql_query($sql,$con);
            if($result){
                if( mysql_affected_rows() > 0 ){
                    $status="success";
                }
            }else{
                $status="failed";
            }
            return $status;
}


  ?>

Upvotes: 0

Views: 180

Answers (5)

Hugo Delsing
Hugo Delsing

Reputation: 14173

Your code has a lot of flaws in it. For instance you are contatenating the string to create a data object. But if somebody would enter a & or = or any other special charactor in it, your form would fail.

Also you are binding on the click function on a button. While this works, it would be useless for people without javascript. This might not be an issue, but its easily prevented with some minor changes.

I would change the <button name="submit" to <input type="submit" and then bind jQuery to the form it self. Also add the action attribute to the form to include 'dbentry.php'

$(function(){
  $('#contact-form').submit(function(){
    var $form = $(this);
    var data = $form.serialize();
    var locurl = 'dbentry.php';

    $.post(locurl,data, function(msg) {
      alert(msg.status)
    }, 'json');

   return false; //prevent regular submit
  });

});

Now to make it work PHP has to return JSON data.

<?php
header('Content-type: application/json');

//your code that includes
echo json_encode(array('status' =>$sql));  

//also notice that your code only returns data on success. Nothing on false.
?>

Upvotes: 0

A. Wolff
A. Wolff

Reputation: 74420

In order to see alert() message, you have to prevent default behaviour of clicked submit button:

$('#form-submit').click(function(e)
    {
        e.preventDefault();
        //....
    }

Otherwise, the FORM is submited and page is reloaded.

Upvotes: 0

Krish R
Krish R

Reputation: 22711

Can you try this,

    var locurl = 'dbentry.php';
    $.ajax({
        url: locurl,
        type:'post',
        data:param,
        dataType:'json',
        success:function(msg)
          {  
              alert(msg.status.sql);            
          }
    });

Upvotes: 0

sumit
sumit

Reputation: 332

Display $status at last in php file instead of return statement You will get it in alert

echo $status;

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

Try to echo it like

if($result){
    if( mysql_affected_rows() > 0 ){
        $status="success";
    }
} else {
    $status="failed";
}
return $status;

And in your if statement code like

if($vals['type'] == "assign_amount")
{
    $values = assign_amount();  
    echo $values;
}

For the ajax return purpose you better to echo or print rather than return it.

Upvotes: 1

Related Questions