We help IT Professionals succeed at work.

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

373 Views
Last Modified: 2012-06-27
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?
Comment
Watch Question

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

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

Author

Commented:
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?
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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?
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Are the variables SHOWN on the page? Or they are just in the code behind?

Author

Commented:
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

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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.
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
Did you tried the way I posted above?

That should work with you perfectly.

Author

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

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
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

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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/
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.