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

TrevorParnhamntl
TrevorParnhamntl used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2004

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

Author

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

Author

Commented:
By removing the form tags it also prevents the vbscript from reading the text box
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Top Expert 2004

Commented:
Move the name attribute from the tag you deleted into the second form tag.

Author

Commented:
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?
Jagadishwor DulalSenior Web Developer

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

Author

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

Most Valuable Expert 2011
Top Expert 2016
Commented:
Please see http://www.laprbass.com/RAY_temp_trevor.php

This code illustrates the design pattern for getting information from radio buttons into your PHP script.  I am not sure what your intent was about the VB script, but I guess if you want that, you could echo the PHP variable into the browser output stream in the middle of the VB script, much as we do here on line 9 of the code example.  If any of this is still not clear to you, please post back with a specific question and I will try to help.

Best regards, ~Ray
<?php // RAY_temp_trevor.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);


// IF THERE WAS ANYTHING POSTED SHOW THE CHOICE
if (!empty($_POST["Prices"]))
{
    echo "<br/>YOU CHOSE {$_POST["Prices"]}";
}


// CREATE THE FORM
$form = <<<ENDFORM
<form name="ValidForm" method="post">
Select Service Required:
<INPUT TYPE="radio"  NAME="Prices" VALUE="1: Fully Trackable Courier" />  Fully Trackable Courier
<INPUT TYPE="radio"  NAME="Prices" VALUE="2: Postal Airmail Service" />   Postal Airmail Service
<INPUT TYPE="radio"  NAME="Prices" VALUE="3: International Signed for" /> International Signed for
<INPUT TYPE="submit" VALUE="Assign">
</FORM>
ENDFORM;


// WRITE THE FORM
echo $form;

Open in new window

Author

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

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial