0

I have a form which passes the forn field values to an ajx script which then passes those values to php and they are they sent to an email address.

The problem i have is that the values are not showing and instead i get the word undefined.

Here is my ajax code.

$(document).ready(function(){
$('form.save').submit(function () {
var name = $(this).find('.name').attr('value');
var email = $(this).find('.email').attr('value');
var telephone = $(this).find('.telephone').attr('value');
// ...
    $.ajax({
        type: "POST",
        url: "application.php",
        data: "name="+ name +"& email="+ email +"& telephone="+ telephone,
        success: function(){
            $('form.save').hide(function(){$('div.success')});
        }
    });
return false;
});
});

Here is my form

  <form action="" method="post" class="save">

                              <input type="text" name="name"  id="name" clsss="name" value="" placeholder="Name"/>

                              <input  type="text" name="email"  class="email" value="" placeholder="Email"/>


                              <input  type="text" name="telephone"  class="telephone" value=""placeholder="Telephone"/>

                            <input name="certified" type="checkbox" value="" checked>I am a Certified sophisticated Investor
                            <input type="hidden" name="type" value="Certified sophisticated Investor">
                            <input type="submit" name="submit" class="button" value="Submit"/>

                           </div>
                      </form>

And then the php (There is no sanitation on the php code i am just trying to get the value and will then use better php code)

 $name = $_POST['name'];
 $email = $_POST['email'];
 $telephone = $_POST['telephone'];


 // mail
 $msg = "$name $email $telephone";
 $msg = wordwrap($msg,70);
 mail("*************","Application",$msg);
3
  • 2
    There is typo in your code , u have used clsss="name" instead of class="name" Commented May 9, 2016 at 10:49
  • why there space after ever & instead of this & email use this &email Commented May 9, 2016 at 11:03
  • @samWalker, try to use serialize function for sending your data Commented May 9, 2016 at 11:09

6 Answers 6

1
data: "name="+ name +"& email="+ email +"& telephone="+ telephone

Remove the spaces

data: "name="+ name +"&email="+ email +"&telephone="+ telephone
Sign up to request clarification or add additional context in comments.

Comments

0

There are few things wrong in your code,

  • See this statement here,

    <input type="text" name="name"  id="name" clsss="name" value="" placeholder="Name"/>
                                                ^ It should be class
    
  • You're getting the input field values in the wrong way. It should be like this:

    var name = $(this).find('.name').val();
    var email = $(this).find('.email').val();
    var telephone = $(this).find('.telephone').val();
    

So your AJAX request should be like this:

$(document).ready(function(){
    $('form.save').submit(function () {
    var name = $(this).find('.name').val();
    var email = $(this).find('.email').val();
    var telephone = $(this).find('.telephone').val();
        $.ajax({
            type: "POST",
            url: "application.php",
            data: {name:name, email:email, telephone:telephone},
            success: function(){
                $('form.save').hide(function(){$('div.success')});
            }
        });
    return false;
    });
});

Comments

0

You can create an object of key/value pairs :

$.ajax({
    ...
    data : { 'name' : name, 'email' : email , 'telephone' : telephone},
    ...
});

Comments

0

send data to ajax as below :

$.ajax({
    ...
    data : { 'foo' : bar, 'bar' : foo },
    ...
});

Comments

0

Try this. It will work fine.

$(document).ready(function(){
$('form.save').submit(function () {
var str = $( "form.save" ).serialize();


$.ajax({
    type: "POST",
    url: "getajax.php",
    data: str,
    success: function(){
        $('form.save').hide(function(){$('div.success')});
    }
});
return false;
});
});

Comments

0

Try this:

 <form id="order-form">    <!--Try to add id for your form -->
    <!--your code here-->
    ...
    ...
 </form>

jQuery:

 $(document).ready(function(){
      $('form.save').submit(function () {
        var data = $("#order-form").serialize();  //You should use serialize for send data
          $.ajax({
            url: "/yourUrl",
            data: data,       
            type: 'GET',
            dataType: 'html',
            success: function (result) {
                 $('form.save').hide(function(){$('div.success')});
                 $("#renderarea").html(result); 
             }
            }); 
            return false;
        });
    }):

Comments

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.