Simplest Post/Get using PHP...ever!

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
1 post Page 1 of 1
Contributors
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

this is so simple a caveman can do it.

this is a simple way to Post data to a php script and get a response back from that script.

first off create a PHP script that looks like this and place it on your server.
Code: Select all
<?php
//turn off all error reporting uncommit the below line
//error_reporting(0);

//this line will send back any errors in the script
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

//use a switch case so you can use a single php file instead of dozens
switch($_GET['sw']){
	case "a":
		echo "I am SWITCH(A) in page.php";
	break;
////////////////////////
	case "b":
		echo "I am SWITCH(B) in page.php";
	break;
///////////////////////
//this sends back an error if the switch() is incorrect
default:
	echo "error";
}
?>
ok now start a new project and add a textbox so you can read the server response.

create a new class file. name it "PostGetClass.vb"
paste this code into a new function.
Code: Select all
Imports System.Net
Imports System.IO

Public Class PostGetClass

    Public Function GetPlayerShipReport(PostURL As String, switch As String)
        Dim request As WebRequest = WebRequest.Create(PostURL + switch)
        request.Credentials = CredentialCache.DefaultCredentials
        Dim response As WebResponse = request.GetResponse()
        'Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
        Dim dataStream As Stream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        Return responseFromServer
        reader.Close()
        response.Close()
    End Function

End Class

and 3 radiobuttons to send or POST the data to the server.
Image
should look something like this.

ok in the code behind Form1 should look something like this.
Code: Select all
Public Class Form1


    Public PGC As New PostGetClass

    Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
        TextBox1.Text = ""
        Dim xURL As String = "http://DOMAIN.COM/page.php"
        Dim switch As String = "?sw=a"
        TextBox1.Text = PGC.GetPlayerShipReport(xURL, switch)
    End Sub

    Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
        TextBox1.Text = ""
        Dim xURL As String = "http://DOMAIN.COM/page.php"
        Dim switch As String = "?sw=b"
        TextBox1.Text = PGC.GetPlayerShipReport(xURL, switch)
    End Sub

    Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
        TextBox1.Text = ""
        Dim xURL As String = "http://DOMAIN.COM/page.php"
        Dim switch As String = "?sw=c"
        TextBox1.Text = PGC.GetPlayerShipReport(xURL, switch)
    End Sub


End Class
run your project (F5)
when you click a RadioButton it should send back the echo"" in the php switch().


if you wish to use my page.php cause you dont have a server
the url is http://scottie1972.dyndns.org/_api/
Simple Post and Get using PHP.zip
You do not have the required permissions to view the files attached to this post.
Image
1 post Page 1 of 1
Return to “Tutorials”