-5

i have a simple form like this:

 <form method="POST" action="do_something.php">
            <label for="name">Your name: </label> <input type="text" id="name" name="name">
            <input type="submit">
        </form>

and then a php file that do this:

<?php
    echo "your name is ". $_POST['name'];
?>

And my quest is to enabled default form sending and to write my own form sending method in javascript. But I'm not a big friend of javascript and i absolutely dont know where to start. :( Can anyone give me direction or help me? Thanks!

user2202284
  • 287
  • 2
  • 13
  • I'd recommend starting with some research; you need to demonstrate that you've already put some work into trying to figure it out for yourself if you want any real help from anyone. – DiMono Feb 04 '14 at 22:02

2 Answers2

2

you are looking for Ajax Requests http://en.wikipedia.org/wiki/Ajax_%28programming%29

Client Side:

// This is the client-side script

// Initialize the Ajax request
var xhr = new XMLHttpRequest();
xhr.open('get', 'send-ajax-data.php');

// Track the state changes of the request
xhr.onreadystatechange = function(){
    // Ready state 4 means the request is done
    if(xhr.readyState === 4){
        // 200 is a successful return
        if(xhr.status === 200){
            alert(xhr.responseText); // 'This is the returned text.'
        }else{
            alert('Error: '+xhr.status); // An error occurred during the request
        }
    }
}

// Send the request to send-ajax-data.php
xhr.send(null);

Server Side:

<?php
// This is the server-side script

// Set the content type
header('Content-Type: text/plain');

// Send the data back
echo "This is the returned text.";
?>
CaffeineAddiction
  • 803
  • 1
  • 14
  • 29
  • `-1` The OP requested no javascript. – qwertynl Feb 04 '14 at 22:11
  • 1
    @qwertynl No, OP said """not a big friend of javascript and i absolutely dont know where to start""" – CaffeineAddiction Feb 04 '14 at 22:13
  • 1
    OP is wrong. Pointing him to the right direction is probably the best way to go. – Florian Margaine Feb 04 '14 at 22:14
  • ok, i can't answer so here is screenshot with my answer: http://d.pr/i/8fJj – user2202284 Feb 04 '14 at 22:36
  • @user2202284 Well, I am not exactly sure what caused that error, but it most likely has something to do with the fact that you are not sending name as a variable with the Ajax Request. For instance, look at the top of the page we are on now stackoverflow.com/quest/numb/post/numb?noredirect=1 in this case noredirect is the variable and 1 is its value. You need to send your php the variable name along with a value. – CaffeineAddiction Feb 04 '14 at 22:46
  • yep, error:0 means XMLHttpRequest status 0 (responseText is empty) http://stackoverflow.com/questions/5005960/xmlhttprequest-status-0-responsetext-is-empty – CaffeineAddiction Feb 04 '14 at 22:51
  • ok, then I understand where the problem is. But how can i set the GET value? Or just to put some variable in html.. – user2202284 Feb 04 '14 at 23:34
  • @user2202284 `xhr.open('get', 'send-ajax-data.php?and+your+variables+go+here');` – CaffeineAddiction Feb 05 '14 at 02:13
0

The only client side code that exists is javascript.

That is the only thing you can use to play around with form submitting.

You can use any server side language you want, it does not have to be PHP.

qwertynl
  • 3,912
  • 1
  • 21
  • 43