0

I've been trying to make a webform for a bit now and am constantly running into troubles behind the server processing portion (where the inputted data from the forms get sent to the server and receive a response from the server).

I think most people in my level of experience (I still classify myself as pretty new to webdev) can get a basic webform created relatively easily, however if the textboxes don't send any of the inputted data anywhere, you won't have a functional webform. That's where I want to learn the next step behind designing webforms.

I can usually find a lot of resources on how to build a form using textboxes, dropdowns, basically any elements and so forth. What I have a hard time trying to find are actual servers I can test my inputted data and receive back a response from the server, and somehow parse it onto a comment box just so I can have a functional conceptualization of my web form.

I would like to basically get something as simple as a web form that asks you to submit a number between 1-24 and then have it return the corresponding letter in the alphabet. So if you submit over the number 1, you will receive a response back from the server "a" and so forth. I don't want the list of choices to be client-side, I want it in the server so I can make a post.

If it helps, I have access to Microsoft Azure, so if spinning up a server or VM of some sort helps me with the server-processing portion, let me know. It would be my first time doing this.

Lasagna Cat
  • 297
  • 3
  • 24
  • Step 1. Choose a server side language you'd like to work with. PHP, Perl, Python, Java, Javascript/Node, etc. Each of those languages have documentation for sending email. – Matt Feb 18 '16 at 22:57

1 Answers1

1

This is a pretty in-depth question, but I can give some pointers.

You will need to pick a server (Windows, Apache, Glassfish, etc), pick a server-side language compatible with your server (PHP, ASP, Python, Java), install that language, study up, and trial-and-error with help from your friends at SO.

However, I will detail one way you can get practicing pretty quickly.

Head over to c9.io (web-based IDE). Create a free account and sign in.

Create a new workspace. Give it a name and description (you can have one free private workspace, for more, you will have to pay. You can, however, make many public ones - in a testing case, it doesn't really matter). Choose a template from the list and click Create Workspace. For this demo, please choose PHP, Apache & MySQL.

Once created, you should see your workspace on the left, along with the files contained in it. You should have a hello-world.php, readme.txt, and php.ini in the workspace. You can read the readme, and delete the hello-world.php (or rename it to whatever your server page should be).

For this, I deleted hello-world.php, and created two files: index.php and server.php (index.php for the frontend and server.php for the backend). In index.php, copy and paste this code:

<!doctype html>
<html>
    <head>
        <title>Test Index</title>
    </head>
    <body>
        Number: <input id='number'>
        Letter: <input id='results' readonly>
        <script src='https://code.jquery.com/jquery-1.12.0.min.js'></script>
        <script>
            $('#number').on('keyup', function(){
                $.post(
                    'server.php',
                    {number:$('#number').val()},
                    function(data){
                        $('#results').val(data);
                    }
                );
            });
        </script>
    </body>
</html>

That's a pretty basic page, but will have your input for a number, and will have a readonly input to display the results from the server. The JS sets up a keyup listener on the number input, and uses jQuery's POST method to send the inputted number to the server. The first parameter is the server page, the second is an object with any information necessary to the server, and the third is a function stating what to do with the information when it returns to the page. This completes the client-side page.

Open the server.php file (the server-side page), and copy and paste the following:

<?php
    function toLetter($number) {
        $arr = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
        return $arr[$number - 1];
    }
    if(isset($_POST['number'])){
        if(is_numeric($_POST['number']) && $_POST['number'] >= 0 && $_POST['number'] <= 26){
            echo toLetter($_POST['number']);
        }
        else{
            echo "Please enter a number between 1 and 26";
        }
    }
    else{
        echo "No input detected.";
    }
?>

This page contains one function (toLetter), and accepts a parameter passed via the POST method called 'number'. If this number is set, is numeric, and is between 0 and 26 (inclusive) it will use the toLetter function to send the corresponding letter in the alphabet back to the client-side page (which will in turn display the results in the input #results). If the number is not sent (a POST is submitted without any data) it will return the message "No input detected.". If the number is invalid (too high, too low, or NaN), it will return the message "Please enter a number between 1 and 26".

Once you've saved these two pages in c9, you can press the "Run Project" button at the top to start the virtual server up. Once it's running (you can see the console at the bottom of the screen), click Preview > Preview Running Application.

You should see two inputs, one labeled Number and one Letter. If you enter a number between 1 and 26 in the Number field, you will see the corresponding letter of the alphabet.

This will get you started with the front-end back-end situation; at least with HTML, AJAX, and PHP. If you use GET instead of POST, you would look in PHP's $_GET array instead of the $_POST array. You can send as much data as you want in the object in the second parameter of $.post. If you use a database on the backend make sure you learn prepared statements, or at the least input sanitization first.

Next step. Generate some awesome client-side to server-side code until it breaks... then come back here with some code for your next question :)

EDIT: tl;dr - Use jQuery AJAX to send to PHP from client. Use $_POST or $_GET to read variables. Use echo to send data back.

  • Wow, this is an amazing answer. Thank you so much, I feel like you totally understood where I was coming from and explained it beautifully. I was able to get the entire answer set up on c9.io and I can now visualize the concept behind the server-processing portion. – Lasagna Cat Feb 19 '16 at 17:09
  • No problem. Glad I could help! I appreciate the upvote, but marking a 'correct answer' does grant a few extra reputation if you do decide to do so... either way, cheers! :) –  Feb 19 '16 at 17:17
  • Another question if I could: If I had a much larger data set (instead of the alphabet), how could I reference it into the server code? For an example, I have a large Microsoft Access DB and want to keep my data set modular in which I can modify the contents of the Access DB and use its interface while not having to dive into the code itself. Is this possible? I'm open to other suggestions or alternatives, I wouldn't know the go-to strategy for this for webdevs or dba's on how they handle data sets. – Lasagna Cat Feb 19 '16 at 17:18
  • I would suggest using a more stable database like SQL or MySQL (MySQL is free - that's what I use). But if you are SET on using Access as your database, you can use PHP to connect and modify data... just use an ODBC or PDO connection: http://stackoverflow.com/questions/19807081 –  Feb 19 '16 at 17:33