how to submit variable to a php page?


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

saleemkhanAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
kennethxuConnect With a Mentor Commented:
You might be right, Javier! It was silly. I realized that you onload idea should have worked but there was some typo
try this:
<%
String un=null;
                String pass=null;
                un=request.getParameter("un");
                // out.print("un="+un);
                pass=request.getParameter("pass");
                // out.print("pass="+pass);
                int result=0;
               if ("un1".equals(un) && "un1".equals(pass))
                {
                  result =1;
                }
               else
               {
                  result=0;
                }
    response.sendRedirect( "http://xyz.com/xyz/result.php?result=" + result );
            } catch (Exception e) {

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

            }
%>
0
 
jarasaCommented:
What are you expecting to get on the php a boolean, a textString or a number?

Javier
0
 
saleemkhanAuthor Commented:
hi jarasa,
  i am trying to print the value in result.php.its a boolean value.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
objectsCommented:
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();
0
 
saleemkhanAuthor 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


0
 
objectsCommented:
> what the above code will do?

It reads the results from the php page and returns it to browser.
0
 
saleemkhanAuthor 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?
0
 
objectsCommented:
> Am i in right direction?

Doesn't sound like it, at the moment your jsp page is calling your php page.
0
 
saleemkhanAuthor 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.
0
 
jarasaCommented:
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
0
 
saleemkhanAuthor Commented:
hi javier,
    i tried your comment. it just prints username and password
 process.jsp and thats all.


where i am wrong.
0
 
jarasaCommented:
What happens if you put on your browser directly the URL http://xyz.com/xyz/result.php?result=true?

Javier

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

0
 
saleemkhanAuthor Commented:

if i put the url directly i will print

result=0
0
 
jarasaCommented:
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());

            }
0
 
saleemkhanAuthor Commented:
compile error cannot invoke toString on a primitive type boolean
at line
String myurl="http://xyz.com/xyz/result.php?result="+result.toString();
0
 
jarasaCommented:
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

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

0
 
jarasaCommented:
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
0
 
saleemkhanAuthor 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.

0
 
jarasaConnect With a Mentor Commented:
OK I checked this one, :c)

<%
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?"0":"1");
out.print("myurl="+myurl);
response.sendRedirect(myurl);

%>

Javier
0
 
kennethxuCommented:
use _GET instead of _REQUEST in your php!
HTH
0
 
jarasaCommented:
>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
0
 
jarasaCommented:
>Can't be that Silly :c))

I mean the solution not you!!!

:c)

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

Javier
0
 
kennethxuCommented:
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 :-)
0
 
jarasaCommented:
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

0
 
kennethxuCommented:
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 :-)
0
 
jarasaCommented:
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
0
 
saleemkhanAuthor 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 -:)
0
 
jarasaCommented:
Is OK :c)
Thanks
Javier
0
 
saleemkhanAuthor Commented:
your welcome -:)

any contact email -:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.