Avatar of TrevorParnhamntl
TrevorParnhamntl
 asked on

passing variables from PHP/html to windows form in vb 2008

I have a project where a php/html document queries a mysql database, performs calculations on the result of the query but then need to pass variables created as part of this to a windows form created in VB 2008.
How do I do this?
PHPVisual Basic.NETHTML

Avatar of undefined
Last Comment
TrevorParnhamntl

8/22/2022 - Mon
Brad Brett

You need to use WebClient to make HTTP request and handle the response by your application:
http://www.dotnetperls.com/webclient-vbnet
TrevorParnhamntl

ASKER
That looks interesting, but doesn't actually answer my question. Can you give an example of how this would be impelmented in a windows form?
Brad Brett

Imports System.Net

Open in new window


Dim client As New WebClient
Dim value As String = client.DownloadString("http://www.example.com")
MsgBox(value)

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
TrevorParnhamntl

ASKER
This gets the source code. The question I'm asking is how do I get the value of a variable. For example, I have a php variable called $country. How do i find the name of the country that variable contains?
Brad Brett

Simply, you need to create a PHP script that use echo() to output the value.

Example:
script.php
<?php
$country = "EG";
echo $country;
?>

Open in new window


Now you can request the contents by using WebClient in your application:
Dim client As New WebClient
Dim value As String = client.DownloadString("http://www.yourwebsite.com/script.php")
MsgBox(value)

Open in new window


The above code should output EG.
Nasir Razzaq

Are the variables SHOWN on the page? Or they are just in the code behind?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
TrevorParnhamntl

ASKER
Meddo337
The simple script that you created does work.
 It seems that because I have a mixture of html and php none of the php is shown only the html
TrevorParnhamntl

ASKER
codecruiser

Do you mean are the php variables echo'd to the screen? some are and some aren't. I need to retrieve some of both
ASKER CERTIFIED SOLUTION
Nasir Razzaq

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
TrevorParnhamntl

ASKER
Thanks code cruiser. I'm going to look at these various options over the weekendans see which might work best. The problem I have is that the hosting company I use won't allow remote access to Mysql, so any calls on the server have to be from their site.
They also only support php not .net.
Maybe it's time to change host.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Brad Brett

Did you tried the way I posted above?

That should work with you perfectly.
TrevorParnhamntl

ASKER
Medo3337
Yes, I did and replied. Croll up to see my reply

Brad Brett

When you use echo() to output any value it output only the data you want (no HTML), for example:
<?php
echo "Hello World!";
?>

Open in new window


This should only output "Hello World!"

If you want to output the variable, use:
echo $variableName

Open in new window


and If you want to output more than one variable, you can use delimiter to split the data, and then you can convert your data to array when you get it in your .NET application:

<?php
$var1 = "Value1";
$var2 = "Value2"
$var3 = "Value3"
echo $var1 . "|" . $var2 . "|" . $var3;
?>

Open in new window


In the above code, I'm using the char | to split the data, this should output "Value1|Value2|Value2", when you get that data in VB.NET using WebClient, you can split it as the following:
Dim client As New WebClient
Dim value As String = client.DownloadString("http://www.yourwebsite.com/script.php")
Dim dataSplit() As String = Split(value, "|")
MsgBox(dataSplit(0)) ' Output Value1
MsgBox(dataSplit(1)) ' Output Value2
MsgBox(dataSplit(2)) ' Output Value3

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
TrevorParnhamntl

ASKER
Medo3337
This would probably work but the issue I have is that when I download the string none of the echo'd data is in it.

Below is a sample of my source code:
This produces two dropdown lists and some input boxes which the user completes.
I then retrieve some of the information in variables in order to do some calculations.

At the bottom of my code sample you will see
<?php
if(isset($_POST['submit']))
{
$country=$_POST['country_name'];
echo $country;
//$Fixed=$_POST['FixPri'];
$weight=$_POST['weight'];    
$length = $_POST['length'] ;
$width = $_POST['width'] ;
$depth = $_POST['depth'] ;
$a= '('.$length.'*'.$width.'*'.$depth.')/5000';
$c = ($length * $width * $depth)/5000;
$lastname=$_POST['Last_Name'];
$b = $c;
If ($weight>$c){
      $b=$weight;
}
?>
when the page is run the value of the variable $country is echo'd to the screen, but in the download string in VB 2008 none of the php code is shown

Here is the full code sample
<FORM method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php            

echo "<select name='country_name'>\n";
while ($data = mysql_fetch_array($res, MYSQL_ASSOC))
{
   $data_array[] = array($data['country_name'],$data['Pforce_zone'],$data['RMIE1'],$data['Fixpri'],$data['GlobPriority'],$data['RMDSItem'],$data['RMDSKilo'],$data['MHIDSItem5u'],$data['MHIDSKilo5u'],$data['MHIDSItem5o'],$data['MHIDSKilo5o'],$data['MHIItem2u'],$data['MHIKilo2u']);

      if ($_POST['country_name'] == $data['country_name'])
    {
        echo " <option value='{$data['country_name']}' selected>{$data['country_name']}</option>\n";        
    }
    else
    {
        echo " <option value='{$data['country_name']}'>{$data['country_name']}</option>\n";
    }
}
echo "</select>\n";
echo "<select name='Last_Name'>\n";
while ($Custdata = mysql_fetch_array($Custres, MYSQL_ASSOC))
{
   $Custdata_array[] = array($Custdata['First_Name'],$Custdata['Last_Name'],$Custdata['eMailPaypal'],$Custdata['Discount']);

      if ($_POST['Last_Name'] == $Custdata['Last_Name'])
    {
        echo " <option value='{$Custdata['Last_Name']}' selected>{$Custdata['Last_Name']}</option>\n";        
    }
    else
    {
        echo " <option value='{$Custdata['Last_Name']}'>{$Custdata['Last_Name']}</option>\n";
    }
}
echo "</select>\n";
?>
            Weight(kgs):<input id="weightid" name="weight" type="text" Size="8" value="" >
              Length(cms):<input id="lengthid" name="length" type="text" Size="6" value="">
               Width(cms):<input id="widthid" name="width" type="text" Size=6 value="">
             Depth;(cms)<input id="depthid" name="depth" type="text" Size=6 Value="">
<input type="submit" id="submitid" name="submit" value="Calculate">            
</form>

<?php
if(isset($_POST['submit']))
{
$country=$_POST['country_name'];
echo $country;
//$Fixed=$_POST['FixPri'];
$weight=$_POST['weight'];    
$length = $_POST['length'] ;
$width = $_POST['width'] ;
$depth = $_POST['depth'] ;
$a= '('.$length.'*'.$width.'*'.$depth.')/5000';
$c = ($length * $width * $depth)/5000;
$lastname=$_POST['Last_Name'];
$b = $c;
If ($weight>$c){
      $b=$weight;
}
?>
<h3> <?php echo "Actual Weight is $weight kgs    Volumetric weight is $b kgs ".'<br/>';
echo 'Destination: '.$country. '; Weight: '.$weight.'kgs'.'<br/>';  
echo 'Dimensions:  '. $length. ' x '.$width. ' x '. $depth.'(cms)';?></h3>
<?php
Brad Brett

Is that the PHP output that you want to get in your application?

<?php echo "Actual Weight is $weight kgs    Volumetric weight is $b kgs ".'<br/>';
echo 'Destination: '.$country. '; Weight: '.$weight.'kgs'.'<br/>';   
echo 'Dimensions:  '. $length. ' x '.$width. ' x '. $depth.'(cms)';?>

Open in new window

Brad Brett

If your PHP script echo HTML, you will have to parse the HTML in your .NET application:
http://www.codeproject.com/KB/vb/Parsing_HTML.aspx
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
TrevorParnhamntl

ASKER
I think I have at least part solved my problem.
The difficulty was that whichever solution I tried would not reveal the php variables because they didn't appear in the source code when downloaded using webclient.
However, they do appear via 'view source in IE.
Therfore using a webbrowser in VB 2008,
Dim value As String = WebBrowser1.DocumentText
Reveals all the source code including the value of the variables.
Therefore all I now need to do is parse this string to extract the information I need.
I think the reason that webclient doesn't reveal the information is because I have a mixture of php and html and it just ignores any php.
Nasir Razzaq

Yes PHP is rendered by Browser and downloadstring does not render it.

You can use the HTML Agility Pack to parse the values

http://htmlagilitypack.codeplex.com/
SOLUTION
Brad Brett

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
TrevorParnhamntl

ASKER
As neither expert fully solved my problem, but the advice they both gave helped me to solve it myself, I think it is fair to split the points
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.