Jump to content




Website help-sending variables


  • You cannot reply to this topic
14 replies to this topic

#1 Exerro

  • Members
  • 801 posts

Posted 18 February 2013 - 02:06 AM

I can't figure out how to send variables to websites through cc. I don't really know how websites work and i don't know much about php but i can learn very quickly.
I know about http.post etc and for some reason that is supposed to send the variables to the website which i understand kind of but i'm not sure what i would have to put in the website after that to receive the variables. Does it do it automatically?

For example would [size=4]
[/size]
http.post(mywebsite,"$var=\"hello\"")
be able to be used on the website as var or does it not work like that

Please help :P

#2 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 18 February 2013 - 02:12 AM

Sending things to a PHP script is easy - you just have to follow a simple format.

Example (in Lua using http.post):
-- This is the variable I want to send to the PHP script. Notice the spaces and punctuation. This must be encoded
local var = "hello there good sir!"

-- The var inside the var= bit is the name you access this variable with when in PHP
local response = http.post("http://www.example.com/script.php", "var=" .. textutils.urlEncode(var))
local contentsOfHTML = response.readAll()
response.close()

This will send the variable var to the script script.php. In the PHP script, you can access it like this:
<?php
// myvariable is the name of the variable containing the string
// var is the key of the array that we supplied when using http.post in lua
$myvariable = urldecode($_POST["var"]);
echo $myvariable;
?>

If we want to send multiple variables, change the http.post to this:
response = http/post("http://www.example.com/script.php", "var=" .. textutils.urlEncode(var) .. "&secondvar=" .. textutils.urlEncode(secondLuaVariable))

And in PHP:
$myvariable = urldecode($_POST["var"]);
$mysecondvariable = urldecode($_POST["secondvar"]);
echo $myvariable."\n";
echo $mysecondvariable;


#3 Exerro

  • Members
  • 801 posts

Posted 18 February 2013 - 02:18 AM

ok thanks...is $_POST an automatic variable for whatever it receives from a post function

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 18 February 2013 - 02:18 AM

If you have a PHP script that has text boxes, you can do what I mentioned in Mikk809h's thread:

local name = "Your Name"
local email = "[email protected]"
local message = "This is my message"
local website = "http://Your_website_script_page.com"

local params = string.format("your_name=%s&your_email=%s&your_message=%s&contact_submitted=send", name, email, message)
-- The text box where you would enter your name is called 'your_name'
-- The text box where you would enter your email is called 'your_email'
-- The text box where you would enter your message is called 'your_message'
-- The 'send' button is called contact_submitted which is needed for a php script with a contact form or something.

local res = http.post(website, params) -- attempts to post it

if res then
    print("Success!")
    local content = res.readAll()
    res.close()
    -- This part you would get the success message using string.find or whatever
else
    print("Failed!")
end

EDIT:

View Postawsumben13, on 18 February 2013 - 02:18 AM, said:

ok thanks...is $_POST an automatic variable for whatever it receives from a post function

$_POST is used to collect variables sent from a form with the method 'post'.
http://www.w3schools...hp/php_post.asp

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 February 2013 - 02:18 AM

View Postawsumben13, on 18 February 2013 - 02:18 AM, said:

ok thanks...is $_POST an automatic variable for whatever it receives from a post function
you have to use $_get if your gunna use http.get

#6 Exerro

  • Members
  • 801 posts

Posted 18 February 2013 - 02:38 AM

yay i got it to work kinda...
website:
Spoiler
computer:
Spoiler


#7 Exerro

  • Members
  • 801 posts

Posted 18 February 2013 - 02:50 AM

now i'm a bit stuck on arrays...how do you add to them?
is there a table.insert() equivalent

#8 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 18 February 2013 - 02:54 AM

-- Define an array as '$myArray'
$myArray = array('First index', 'Second', 'Third');

-- Loop through all the things in the array
foreach($myArray as $val) {
	echo $val;
}


#9 Exerro

  • Members
  • 801 posts

Posted 18 February 2013 - 02:57 AM

i know that but what if you want to have an unlimited amount of users? im thinking i have to use files to save them

#10 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 18 February 2013 - 02:59 AM

View Postawsumben13, on 18 February 2013 - 02:57 AM, said:

i know that but what if you want to have an unlimited amount of users? im thinking i have to use files to save them
Look into MySQL. It's rather easy and a very good solution. It's easy to use from PHP.

#11 Exerro

  • Members
  • 801 posts

Posted 18 February 2013 - 03:04 AM

congrats on the 1000 < posts and whats MySQL? another programming language?

#12 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 18 February 2013 - 03:10 AM

View Postawsumben13, on 18 February 2013 - 02:57 AM, said:

i know that but what if you want to have an unlimited amount of users? im thinking i have to use files to save them

Oh ...

View PostOrwell, on 18 February 2013 - 02:59 AM, said:

View Postawsumben13, on 18 February 2013 - 02:57 AM, said:

i know that but what if you want to have an unlimited amount of users? im thinking i have to use files to save them
Look into MySQL. It's rather easy and a very good solution. It's easy to use from PHP.

Yes, please don't use files X_X

You're using 000webhost right? They do support PHP with free accounts so go to phpMyAdmin. But first you need to create a PHP database with a username and a password.

In the members area, click on MySQL under 'Software / Services' with the desired database name, username and password. Now go into phpMyAdmin to add tables with different fields etc.

To connect to a MySQL DB:
$con = mysql_connect("webhost", "username", "password") or die('This error message will appear if it failed to connect the the DB'); 
Now that will just initialize the connection, to select a database:
mysql_select_db("database_name", $con) or die(mysql_error());
where database_name is the name of the database you want to send/retrieve information from/to.

From then on, just google the different queries you can do when dealing with databases.

#13 Exerro

  • Members
  • 801 posts

Posted 18 February 2013 - 03:13 AM

erm....sound confusing....wouldnt it do the same if i had file_get_contents etc? php and websites is so much harder than i thought :l

#14 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 18 February 2013 - 03:49 AM

View Postawsumben13, on 18 February 2013 - 03:13 AM, said:

erm....sound confusing....wouldnt it do the same if i had file_get_contents etc? php and websites is so much harder than i thought :l

Pleassseeeeee don't go with a plain file solution. It's actually far harder to protect/use, even though it may seem simpler.

@RemiX, you know mysql is depreciated? You're ment to use mysqli.

#15 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 18 February 2013 - 04:15 AM

View PostGravityScore, on 18 February 2013 - 03:49 AM, said:

View Postawsumben13, on 18 February 2013 - 03:13 AM, said:

erm....sound confusing....wouldnt it do the same if i had file_get_contents etc? php and websites is so much harder than i thought :l

Pleassseeeeee don't go with a plain file solution. It's actually far harder to protect/use, even though it may seem simpler.

@RemiX, you know mysql is depreciated? You're ment to use mysqli.

Never heard of MySQLI before o_O Well the free website hosting I use doesn't look like they support/offer it.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users