user1261800
user1261800

Reputation:

Posting input field value through JS Function

I am having difficulties in returning an input field value through a JS function. It does return a value but it adds unnecessary information. How can I get echoed out the input value without other things? EXAMPLE

JS

   <script>
    $(function() {

    $(".submit").click(function() {

        var name = $("#name").val();                
        var dataString = 'name='+ name;

                $.ajax({
                type: "POST",
                url: "jqueryform.php",
                data: dataString,
                success: function(result){
                $('#special').append('<p>' +  result + '</p>')  

                    }

               });
                return false;
           });     

    });
    </script>

PHP

 <?php
 if($_POST){
     $url     = $_POST['name'];
     echo ('What you type:&nbsp;&nbsp; <b>'.$url.'</b>');
    }
 ?>

HTML

<html>
 <input id="name" name="name" type="text" class="field text medium" value="" maxlength="255" tabindex="1"   />

<input  type="submit" value="Submit" style=" background:#0060a1; color:#FFFFFF; font-size:14px; border:1px solid #0060a1; margin-left:12px" class="submit"/>

<div id="special"> </div>

</html>

Upvotes: 1

Views: 184

Answers (3)

Paul Fleming
Paul Fleming

Reputation: 24526

Change echo statement to:

echo ('What you type:&nbsp;&nbsp; <b><span id="resultval">'.$url.'</span></b>');

Change response method body to:

$('#special').append('<p>' +  $('#resultval', result).html() + '</p>');

Upvotes: 1

Blackfish
Blackfish

Reputation: 54

Instead of echoing an plain php Use Json method

echo json_encode($rl);

and in the JS after the ajax call is complete decode the json encoded string and use it

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324820

You appear to be submitting the AJAX to a standard page, complete with the form. You would probably be better off just using a regular <form>, no AJAX, and it'll work exactly how you expect.

Upvotes: 2

Related Questions