We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

how to submit variable to a php page?

saleemkhan
saleemkhan asked
on
Medium Priority
301 Views
Last Modified: 2010-04-01

hi all ,
  i am trying to fetch the data from a php page and
do the backend process in jsp page and then submit the result to php page. i am using URLConnection .

code in login.php

<form action="http://abc.com/process.jsp" method="post">
<input name="un" type="text">
<input name="pass" type="password">
<input type="submit" value="Submit">
</form>


code in process.jsp

String un=null;
                String pass=null;
                un=request.getParameter("un");
                out.print("un="+un);
                pass=request.getParameter("pass");
                out.print("pass="+pass);
                boolean result=false;
               if ("un1".equals(un) && "un1".equals(pass))
                {
                  result =true;
                }
               else
               {
                  result=false;
                }
               
                  String myurl="http://xyz.com/xyz/result.php?result="+result;
                  out.print("myurl="+myurl);
                  URL u = new URL(myurl);
                  URLConnection yc= u.openConnection();
                 BufferedReader html = new BufferedReader(new InputStreamReader(u.openStream()));
                  html.close();

 

            } catch (Exception e) {

                  out.print("There is a connectionproblem: " +e.getMessage());

            }

code in result.php

<?php
echo "<br>";
echo "result=";
echo $_REQUEST['result'];
echo "<br>";
?>


my problem is i am unable to print the result fetch from jsp.i think there is some problem in submitting the variable to a php page? am i in the right way? or guide me where i am wrong?

waiting for expert solution.?

Comment
Watch Question

Commented:
What are you expecting to get on the php a boolean, a textString or a number?

Javier

Author

Commented:
hi jarasa,
  i am trying to print the value in result.php.its a boolean value.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you don't read the results from the php:

  BufferedReader html = new BufferedReader(new InputStreamReader(u.openStream()));
  String line = null;
  while (null!=(line=html.readLine()))
  {
      out.println(line);
  }
  html.close();

Author

Commented:
hi objects,
 >>  you don't read the results from the php:

 >> BufferedReader html = new BufferedReader(new InputStreamReader(u.openStream()));
 >> String line = null;
 >> while (null!=(line=html.readLine()))
  >>{
     >> out.println(line);
 >> }
  >.html.close();

what the above code will do?have u got me problem?my problem is i cant able to print the result in result.php.(i cant see the result value in result.php its blank).

waiting for expert solution


Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> what the above code will do?

It reads the results from the php page and returns it to browser.

Author

Commented:
hi objects,
   i dont want to read results from a php page.i want to throw the result from my jsp page to php.
And in php page i want to display(print) the result.

Am i in right direction?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> Am i in right direction?

Doesn't sound like it, at the moment your jsp page is calling your php page.

Author

Commented:
hi objects,
  Let me explain you what i am trying to do.

1)A php page(login.php) username  and password will submit to my jsp file.
 code :  
<form action="http://abc.com/process.jsp" method="post">
<input name="un" type="text">
<input name="pass" type="password">
<input type="submit" value="Submit">
</form>
2)My jsp page fetch username and password and do process and then submit the result to result.php
 code :
code in process.jsp

String un=null;
                String pass=null;
                un=request.getParameter("un");
                out.print("un="+un);
                pass=request.getParameter("pass");
                out.print("pass="+pass);
                boolean result=false;
               if ("un1".equals(un) && "un1".equals(pass))
                {
                  result =true;
                }
               else
               {
                  result=false;
                }
               
                  String myurl="http://xyz.com/xyz/result.php?result="+result;
                  out.print("myurl="+myurl);
                  URL u = new URL(myurl);
                  URLConnection yc= u.openConnection();
                 BufferedReader html = new BufferedReader(new InputStreamReader(u.openStream()));
                  html.close();



            } catch (Exception e) {

                  out.print("There is a connectionproblem: " +e.getMessage());

            }

 as per your comment i understand that  i am calling a php page.

I think i have to use outputstream or anyother method to submit the result to result.php.

am i right?
if not correct me.


and in result.php i just want to print the value

code in result.php

<?php
echo "<br>";
echo "result=";
echo $_REQUEST['result'];
echo "<br>";
?>

am i right?
if not correct me.

Commented:
Try this to make a test:
<%
String un=null;
                String pass=null;
                un=request.getParameter("un");
                out.print("un="+un);
                pass=request.getParameter("pass");
                out.print("pass="+pass);
                boolean result=false;
               if ("un1".equals(un) && "un1".equals(pass))
                {
                  result =true;
                }
               else
               {
                  result=false;
                }
%>

<html>
<head>
</head>
<Body onLoad="top.location.href='http://xyz.com/xyz/result.php?result='<%=result%>;">
</Body>
</html>                

<%
            } catch (Exception e) {

                  out.print("There is a connectionproblem: " +e.getMessage());

            }
%>

Javier

Author

Commented:
hi javier,
    i tried your comment. it just prints username and password
 process.jsp and thats all.


where i am wrong.

Commented:
What happens if you put on your browser directly the URL http://xyz.com/xyz/result.php?result=true?

Javier

Commented:
Sorry you said a boolean
http://xyz.com/xyz/result.php?result=0
Javier

Author

Commented:

if i put the url directly i will print

result=0

Commented:
And this?

String un=null;
                String pass=null;
                un=request.getParameter("un");
                out.print("un="+un);
                pass=request.getParameter("pass");
                out.print("pass="+pass);
                boolean result=false;
               if ("un1".equals(un) && "un1".equals(pass))
                {
                  result =true;
                }
               else
               {
                  result=false;
                }
               
                  String myurl="http://xyz.com/xyz/result.php?result="+result.toString();
                  out.print("myurl="+myurl);
                  URL u = new URL(myurl);
                  URLConnection yc= u.openConnection();
                 BufferedReader html = new BufferedReader(new InputStreamReader(u.openStream()));
                  html.close();



            } catch (Exception e) {

                  out.print("There is a connectionproblem: " +e.getMessage());

            }

Author

Commented:
compile error cannot invoke toString on a primitive type boolean
at line
String myurl="http://xyz.com/xyz/result.php?result="+result.toString();

Commented:
I can't understand that:

toString() -> Returns a String object representing this Boolean's value.

Opps I see:

Change this:

boolean result=false;

For this

Boolean result=false;

And try again please

Javier

Author

Commented:
i tried your comment,
 i am getting error typemismatch error cannot from boolean to java.lang.Boolean.

Commented:
Ok Lets do this and if it works we try to fix it.

Put this line on your original code:

String myurl="http://xyz.com/xyz/result.php?result=0";

Sorry for the errors but I don't have time to make the validations here.

Javier

Author

Commented:

i tried your comment,

but no success.

explanation:-
Whats happens here is when i submit username and password from login.php it goes to process.jsp
its prints like this
un=un1pass=un1
myurl=http://xyz.com.com/xyz/result.php?result=0

why it wont redirect to result.php and display the result there.

Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
use _GET instead of _REQUEST in your php!
HTH

Commented:
>use _GET instead of _REQUEST in your php!

Can't be that Silly :c))

I have to say that know nothing about PHP tho.

If he is getting the out.print() does not mean that the redirection is not working??

Javier

Commented:
>Can't be that Silly :c))

I mean the solution not you!!!

:c)

Javier
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Commented:
I always have this problem is like the AND && and OR || always mismatch them.
true is 0 and false is 1 or viceversa??

Javier
hmm. I think for shell, 0 is success, otherwise fail. any other language I came across is true-1, false-0. but I could be wrong, I'm silly :-)

Commented:
Well I'm serious Kenneth I alway mismatch that. I say that becose of my previous post:

Should be this way:
String myurl="http://xyz.com/xyz/result.php?result="+(result?"0":"1");
or this way:
String myurl="http://xyz.com/xyz/result.php?result="+(result?"1":"0");

Javier

saleemkhan, Javier's solution is very close to the answer. He should get points not me. (I don't thin i'll get but just in case :-)

Commented:
Kenneth, I'm not here for the points, and besides I made too many obvious mistakes in this question, I guess he should split them.

I've learned too today :c)

Javier

Author

Commented:
hi,
   i split the points.i dont have idea who got 125 and who got 50-:)
i am sorry  if i make a mistake in spliting points -:)

Commented:
Is OK :c)
Thanks
Javier

Author

Commented:
your welcome -:)

any contact email -:)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*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.