Avatar of TrevorParnhamntl
TrevorParnhamntl

asked on 

How to transfer vbscript variable to php variable on the same page?

I have a .php file where I retrieve data from a mysql database, I then use html on the same page to offer options based on the query result.
This is done using a form which is posted back to the same page using php_self.
I then run some calculations using a combination of html and php and then offer the user further options again using a form and php_self. the option the user chooses is posted to a text box (Text1).
I then want to use that information to send back to my database.
The problem I have is that I don't seem to be able to retrieve the information in the text box. I can retrieve it using vbscript. I get an undefined index error. Testing with Isset shows that the variable hasn't been assigned in php
My question is how can I retrieve it using php, either directly from the user choice or from the vbscript variable?
I have included my code
<?php
ini_set('display_errors','1');
error_reporting(E_ALL);
?>

<HTML>
<HEAD>
<SCRIPT LANGUAGE="VBScript"> 
<!--
Sub Submit_OnClick
  Dim TheForm
  Dim Choice
  Set TheForm = Document.ValidForm
    Choice=TheForm.Text1.Value
 if Choice<>"" then
'MsgBox "You Chose  " & Choice
end if
End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="ValidForm">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<INPUT NAME="Text1" TYPE="TEXT" SIZE="20">
Select Service Required:
<INPUT TYPE="radio" NAME="Prices" VALUE="1"
   onClick="Text1.value = 'Fully Trackable Courier'"> Fully Trackable Courier
<INPUT TYPE="radio" NAME="Prices" VALUE="2"
   onClick="Text1.value = 'Postal Airmail Service'"> Postal Airmail Service
<INPUT TYPE="radio" NAME="Prices" VALUE="3"
   onClick="Text1.value = 'International Signed for'"> International Signed for
<INPUT NAME="Submit" TYPE="BUTTON" VALUE="Assign">
</FORM>
<?php
if(isset($_POST['Text1'])){
$var =$_POST['Text1'];
echo $var;
   }
?>
</BODY>
<HTML>

Open in new window

PHP

Avatar of undefined
Last Comment
TrevorParnhamntl
Avatar of Zyloch
Zyloch
Flag of United States of America image

First, you cannot have a form within a form. Remove the outer <FORM NAME="ValidForm"> tag. Second, are you submitting your form? You will only see the PHP echo after the form is submitted.
Avatar of TrevorParnhamntl
TrevorParnhamntl

ASKER

Hello, I have removed the outer form tags, but still get the same error. Yes, I am submitting the form and the variable isn't echo'd

Avatar of TrevorParnhamntl

ASKER

By removing the form tags it also prevents the vbscript from reading the text box
Avatar of Zyloch
Zyloch
Flag of United States of America image

Move the name attribute from the tag you deleted into the second form tag.
Avatar of TrevorParnhamntl

ASKER

Ah, sorry. I now understand. This still doesn't solve the problem. I still get the same error. I can't understand why PHP seems unable to reade the $_POST array.
Do you know of anyway of PHP being able to read the vbscript varaible?
I just posting Some working example here that work perfectly but slightly I have used javascript here because I am from java background hope will help you:

<HTML>
<HEAD>
</HEAD>
<BODY>
    <script type="text/javascript">
      <!--
      function newval()
      {
         var t="";
         for (i=0; i<document.form1.Prices.length; i++)
         {
            if (document.form1.Prices[i].checked==true)
            {
               t =t +document.form1.Prices[i].value
            }
         }
         if (t=="")
         {
            document.getElementById("Text1").value=""
         }
         else
         {
            document.getElementById("Text1").value=t
         }
      }
      -->
      </script>
   </head>
   <body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="form1">
<INPUT NAME="Text1" TYPE="TEXT" id="Text1" SIZE="20">
Select Service Required:
<INPUT TYPE="radio" NAME="Prices" VALUE="Fully Trackable Courier" onClick="newval()"> Fully Trackable Courier
<INPUT TYPE="radio" NAME="Prices" VALUE="Postal Airmail Service" onClick="newval()"> Postal Airmail Service
<INPUT TYPE="radio" NAME="Prices" VALUE="International Signed for" onClick="newval()"> International Signed for
<input type="submit" name="Submit" id="button" value="Assign">

</form>
<?php
if(isset($_POST['Submit'])){
$var=$_POST['Text1'];
echo $var;
}
?>
</BODY>
<HTML>

Open in new window

Avatar of TrevorParnhamntl

ASKER

jagadishdulal

This almost works. If I just make this into a standalone form it works, but because this is part of a larger web page it opens a new instance of the form in a new window and still doesn't display the variable.

I did try this myself some time ago.

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS 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
Avatar of TrevorParnhamntl

ASKER

Ray,
My apologies for not getting back to you sooner.
Your solution pretty well does what I want it to do. I found I could do without the vbscript .
I'm going to accept your solution but then I need to ask another question, which is how to pass variables from php/html to a windows form in VB.Net?

Thanks for your help
Avatar of TrevorParnhamntl

ASKER

It didn't totally solve my problem, but put me on the right lines
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo