0

I am a beginner in java-script, I would like to send my values to another page with using java-script .

my codes :

<form  id="contact-form" method="post" enctype="multipart/form-data">              
   <fieldset>
       <label><span class="text-form">name</span><input name="name"  type="text" /></label>
       <label><span class="text-form">email</span><input  type="text" /></label>   
       <label><span class="text-form">phone</span><input  name="mobile" type="text" /></label>     
       <div class="wrapper"><div class="text-form">comment</div><textarea id="nazar"></textarea></div>
        <div class="buttons">
            <a class="button" href="#" onClick="document.getElementById('contact-form').reset()">refresh</a>
            <a class="button"href="savenazar.php" onClick="document.getElementById('contact-form').submit()">send</a>
        </div>                             
     </fieldset>                        
  </form>

now how can I get name,phone .. values in savenazar.php page ?

MH2K9
  • 11,951
  • 7
  • 32
  • 49
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254

3 Answers3

2

You need to add action="savenazar.php" attribute to the form and use <input type="submit"> instead of <a>. The email textbox also needs name="email" attribute

<form action="savenazar.php" id="contact-form" method="post" enctype="multipart/form-data" >                    
    <fieldset>
        <label><span class="text-form">name</span><input name="name"  type="text" />
        </label>
        <label><span class="text-form">email</span><input name="email" type="text" /></label>   
        <label><span class="text-form">phone</span><input name="mobile" type="text" /></label>     
        <div class="wrapper"><div class="text-form">comment</div><textarea id="nazar"></textarea></div>
        <div class="buttons">
        <a class="button" href="#" onClick="document.getElementById('contact-form').reset()">refresh</a>
        <input type="submit" class="button" value="send" />
        </div>  
    </fieldset>    
</form>

then in savenazar.php you can get the values by using $_POST["x"] where x is the name attribute of the input tag you want to get from. For example you can get name by $_POST["name"] because the name textbox has name="name" attribute. You can get email by $_POST["email"] because the email textbox has name="email" attribute. You can get phone by $_POST["mobile"] because the phone textbox has name="mobile" attribute, and so on.

Alternatively you can also use the <a> tag like in your previous code and set the href attribute to # as mentioned by @Ghost in the comment below

<form action="savenazar.php" id="contact-form" method="post" enctype="multipart/form-data" >                    
    <fieldset>
        <label><span class="text-form">name</span><input name="name"  type="text" />
        </label>
        <label><span class="text-form">email</span><input name="email" type="text" /></label>   
        <label><span class="text-form">phone</span><input name="mobile" type="text" /></label>     
        <div class="wrapper"><div class="text-form">comment</div><textarea id="nazar"></textarea></div>
        <div class="buttons">
        <a class="button" href="#" onClick="document.getElementById('contact-form').reset()">refresh</a>
        <a class="button" href="#" onClick="document.getElementById('contact-form').submit()">send</a>
        </div>  
    </fieldset>    
</form>
ekad
  • 14,436
  • 26
  • 44
  • 46
  • i think he want to use JAVASCRIPT – jmn Nov 29 '14 at 08:00
  • 1
    you could also just leave the anchor that the OP setup, just leave it as `href="#"` then your action is already correct, anyways, +1 – Kevin Nov 29 '14 at 08:02
2

The following code will help you achieve what you are looking for and it does it with AJAX so no need to refresh the page

<form  id="contact-form" method="post" enctype="multipart/form-data">              <fieldset>
   <label><span class="text-form">name</span><input name="name"  id="name" type="text" /></label>
   <label><span class="text-form">email</span><input id="email" type="text" /></label>   
   <label><span class="text-form">phone</span><input id="mobile" name="mobile" type="text" /></label>     
   <div class="wrapper"><div class="text-form">comment</div><textarea id="nazar"></textarea></div>
    <div class="buttons">
        <a class="button" href="#" onClick="document.getElementById('contact-form').reset()">refresh</a>
        <a class="button id="submit">send</a>
    </div>                             
 </fieldset>                        
  </form>

add the following js to your file or include it

$('#submit').click(function() {
name = document.getElementById('name').value;
email = document.getElementById('email').value;
mobile = document.getElementById('mobile').value;
$.post('savenazar.php', {'name': name, 'email': email, 'mobile'" mobile}, function(data) {

   var parsed = JSON.parse(data);

    var html = 'HTML to show when data is passed';
<!-- following are your divs -->
$('#requestStatus').append(html);
 }).success(function() {
    $('#comment').val('');
    $('#sentSuccess').html('data Sent!').show().delay(5000).fadeOut();
}).fail(function() {
    $('#sentFailed').html('data not sent').show().delay(5000).fadeOut();
});

});

// In savenazar.php

<?php
$name = $_GET['name'];
Khan Shahrukh
  • 6,109
  • 4
  • 33
  • 43
0

You would require some knowledge of server side programming, in this instance PHP.

Handling POST data server-side should be taken seriously as this is easily the most prominent security hole in most new websites.

A simple PHP script to view POST data would be:

<?php

if ($_POST) {
    echo '<pre>';
    var_dump($_POST);
    echo '</pre>';
}

?>

"Security measures" must be taken when it comes to ensuring the validity of client-side input

  • Prevent SQL Injection (If storing in MySQL use mysqli_real_escape_string())
  • Form Flooding (Captcha is the most popular choice to prevent this)

Taking what ekad added you can then access the POST data using $_POST['name'] etc.

I recommend looking up some beginner tutorials, here is a good one to get started: PHP Forms

zanderwar
  • 3,440
  • 3
  • 28
  • 46