/    Sign up×
Community /Pin to ProfileBookmark

OK, I’m a server-side newbie! Can anyone help me?

Hello everyone,

I’ve been developing on the client-side for a long time, and I’ve always wanted to do some server-sided stuff, which are essentially things that the client-side is incapable of.

I’ve studied PHP, AJAX, and Node.js…….BUT, when it comes to testing them on an online hosting website (free ones like 000webhost), I get sooooooo freaking lost! There are MANY server-side options to choose from, but I’m a SIMPLE guy. Look, this is all I want to achieve:

-I want to create a webpage that can read a .txt file from the server
-I also want this page to be able to send data to the server (like a numerical value(s), text, etc. likely via <form>)

OKAY, here’s what I want to achieve:
———————————————————

Step #1 – User inputs the number “1” via webpage text input and submits via form
———————————————————


Step #2 – The submitted number will be sent to the server (to a server file that contains the calculation code)
———————————————————

[Server file contents]
Pseudo CODE:


**********************************

variableSentFromWebPage + 2 = x;
sendThisResponseBackToWebpage(x);


**********************************

Step #3 – Display the data response (the value of ‘x’ from the server calculation result) to the user on the webpage.
———————————————————

….as far as basics are concerned, this is what I need, but I’m having a WHOLE WORLD of trouble trying to do this on an online web server (not my personal PC’s private one) host like 000webhost. The site does have PHP/phpMyAdmin/SQL support, but do I REALLY have to go through all of that just for something as simple as the above basic client->server communication example?

Can anyone guide me step by step on what the code should look like (PHP language, if possible) as well as what free web hosting site that you recommend that I signup for to test the code? Seriously, I would like to see this code in action.

Thanks in advance!

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@rootJul 05.2018 — Looks very Homeworky in its detail...
Copy linkTweet thisAlerts:
@ginerjmJul 05.2018 — You said:

"variableSentFromWebPage + 2 = x;"

You also said that you have read up on php and ajax.

Those two statements are in conflict since if you read ANY php or javascript (used perhaps in an ajax call) you would know that your first statement is NOT how you write php or JS.

Why don't you SHOW us what you have tried to write? Your post appears to be a lazyman's way out of actually writing what appears to be a very simple script. Writing a script is not a question of choosing from "so many server-side options" (whatever you meant by that I don't know).

Basically - you write an html page that contains a simple form having an input field and a submit button that calls your script. Then you write a script that checks that it has received a POST request and validates the input coming from your form. Then the php code does what you want it to do with that input and returns back an html page showing results or whatever.
Copy linkTweet thisAlerts:
@TripMXauthorJul 05.2018 — I believe that my post was misunderstood. It's not so much a matter of writing a simple script; I've done this kind of stuff on IIS7.

My biggest issue is getting this 'simple script' to work on a server that isn't my own PC (ala IIS7), e.g. one of the online web hosting company servers. It's not like I could just simply copy and paste my code on there and not expect to encounter issues (e.g. access denied this, no permission that) that I never encountered while testing it on my own server.

The options that I spoke of are like Ruby on Rails, ASP.net, jQuery, MySQL, Python, PHP, etc. <-- these are just a few of the many server-side supporting options.

Like I said, I'm a simple guy. I write all my HTML and JavaScript in Notepad (not Notepad++), and I get results fast because I can test my code both online and offline without being chastised by permission/access woes, as all I need to do is upload the file(s) to the online web hosting company server, and open them successfully without a hitch.

With the server-sided approach, I'm not sure if I even need to use myPhpAdmin or the MySQL database options for something as simple as my PSEUDOCODE entails.

I would like to avoid using those other options and just simply use raw PHP, HTML, and JS to get the job done. This is what I'm asking.
Copy linkTweet thisAlerts:
@NogDogJul 05.2018 — There's no need to use any database interaction for this simple example, but if it's necessary that something be done on the server side, then you'll need a server-side language of some sort. At its simplest, assuming PHP and a HTTP request that would have the number in the 'number' GET param:

``php<i>
</i>&lt;?php
if(isset($_GET['number']) and is_numeric($_GET['number'])) {
$result = array('result' =&gt; $_GET['number'] + 2);
}
else {
$result = array('result' =&gt; null, 'error' =&gt; 'No number was supplied');
}
header('Content-Type: application/json');
echo json_encode($result);
exit;<i>
</i>
``

Using that in connection with any typical JavaScript AJAX function/library/framework/whatever would allow you to access that result from the server as a JSON object, then do whatever you want with it within the DOM of your web page in order to display it (or the error message).
Copy linkTweet thisAlerts:
@TripMXauthorJul 07.2018 — @NogDog#1593594 Thanks so much for the help thus far! Now, I just need guidance for one more thing:

...and that is:

How can I post data from a form on a webpage to a .txt file on the back-end server using such 'simple' methods?
-



I essentially want to be able to post (and SAVE/STORE in same .txt file) data from a webpage form to the server for later reading/writing.

I have attached a sample diagram to this post.[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-07-07/1530941498-417391-savingclient-to-server.png]
Copy linkTweet thisAlerts:
@rootJul 07.2018 — You use a form...

I think you are looking at something that can be as simple as...

You use PHP code and functions to direct the data in to the stream you want, this could be, a database, file, another place like forwarded to another system or output to screen or printer or all of them...

Clue:

file_put_contents() and file_get_contents()

do what?

Then look at other functions for any specific formatting like if your in need of putting that data in to CSV format.

I take it that the text file you write to will need to be in the JSON format? If so, then a simple sending the string from the clients browser as a JSON string already, will only require one action file_put_contents("TEXT.TXT", $_POST['jsonstr'], FILE_APPEND | LOCK_EX ); to write to a TEXT.TXT file the data in $_POST field and append it to the file and lock it (flock) while writing.

Assume that your JSON data is in the textarea already stringified

So your form would look something like this
&lt;form name="myJSON" action="upload.php" method="post" enctype="multipart/form-data" onsubmit="return process( this );"&gt;
&lt;textarea name="jsonstr" rows="5" cols="80"&gt;&lt;/textarea&gt;
&lt;input name="Submit" type="submit" value="Submit"&gt;

The process( this ) function takes the form, allows easier manipulation...
function process( fm ){
// stop the form submission
fm.preventDefault();
// get a handle to work with
var targetField = fm.jsonstr;
// get the data and pack it as a JSON string
var package = JSON.stringify( myobject_or_array );
// wrap it up as a base64 encoded string and update the target field
targetField.value = atob( package );
// submit the form
fm.submit();
}

Your sercer script accepts a string that is base64 that is then decoded and then further processed if needed.
// the target filename
$targetFile = "text.txt";
// check we have a file, if not... make one.
if( !file_exists( $targetFile ) file_put_contents($targetFile,""); // write empty file
// if we have a field filled, do something with it.
if(isset($_POST'jsonstr']){
$string = file_put_contents("text.txt", base64_decode( $_POST['jsonstr'], FILE_APPEND | LOCK_EX );
}

*untested, not recommended for production use and no warranties that it works as intended... etc.

The idea, get your JSON stingified, wrap it in base64 encoding, post it to the server as a text field, decode it, write the JSON string directly to the file, each write will append to the file, handy if you want to create a record of posts.
Copy linkTweet thisAlerts:
@PeterPan_321Nov 30.2018 — I always recommend starting with basics and working from there. In that regard, i would recommend you start with your existing knowledge of plain vanilla Javascript and HTML forms, and from there start by creating a very simple PHP file, to do nothing more than echo the words "Hello World". Then get to the point where you can at least place that PHP file in the hosted file space, and access it within a simple HTML form on that same space. If you do not have permissions to upload or run such a basic PHP file, work on that first with something very basic like this. When you get to the point where you can SUBMIT your form, it calls the PHP module, and the basic message is displayed, THEN proceed to the larger task you're asking about.
Copy linkTweet thisAlerts:
@NogDogNov 30.2018 — Generally not cool to re-open old, inactive threads, unless you have a really good reason?

[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-11-30/1543612988-204267-screen-shot-2018-11-30-at-42225-pm.png]
×

Success!

Help @TripMX spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...