Jump to content


Photo

General questions...I dont know how to explain


  • Please log in to reply
13 replies to this topic

#1 Revos

Revos
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 31 August 2006 - 12:12 AM

this is my code:
<html>
<body>
<form method="post">
<input name="Pass" type="password" maxlength="10" />
<input name="Send" type="submit" value="Ok" />
</form>
<?php 
$Pass = $_POST["Pass"];
if($Pass == "bla")
{
echo "Successfuly connected!";
}
else
{
echo "Wrong password!";
}
?>
</body>
</html>
Now it works fine but when I just open the page is shows me Wrong password!
1]What is the problem?
2]How can I open a page with php? let's say instead of echo "Successfuly connected!" I want it to open a page called XX.php, how?
3]I am not sure this is PHP but I will ask here, I got button called XX how can I make that when I press it the value of text named XY will change to "444"?
Thanks and I am sorry to ask so much but I am a newbie at this..

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 31 August 2006 - 12:19 AM

1/ You need to check the form has been submitted.

<?php
if (isset($_POST["Pass"])) {
  $Pass = $_POST["Pass"];
    if ($Pass == "bla")
    {
      echo "Successfuly connected!";
    } else {
      echo "Wrong password!";
    }
}
?>

2/ Yoy can use the header() function to redirect, or the include() function to include a file.

3/ You are correct.... this sounds like a javascript question, not php.

#3 Revos

Revos
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 31 August 2006 - 12:22 AM

ok, can you explain more about this line:
if (isset($_POST["Pass"])) {
(I am not sure but I think isset is checking if it's null..anyways I would love to hear an explanation)

And about my two other questions, thanks.

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 31 August 2006 - 12:26 AM

It checks to see if the variable $_POST['Pass'] is set. This variable will only be set if your form has been submitted, hance the code within that if{} will only run when the form has been submitted.

#5 Revos

Revos
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 31 August 2006 - 12:31 AM

What do you mean is set?
You mean if it has been submitted then start the if part?
ok, I will ask in JS forum, are you sure it's JS and not HTML?
by the way, thanks  :)

#6 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 31 August 2006 - 12:34 AM

What do you mean is set?


Bassicaly, $_POST['Pass'] does not exists (is NOT set) unless your form is submitted.

As for question 3. It could be done in php but sound slike it would be much better suited to a client side tech like javascript. Can you post an example of what you mean?

#7 Revos

Revos
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 31 August 2006 - 12:46 AM

I can't find it right now but here I will explain:
I made button:
<input name="Change" type="button" value="Change" />
Now I also made a text box:
<input name="ChangedTxt" type="text" />

What I am willing to do it when I press the button Change the value of ChangedTxt will be "123"

About my second question, I tried this code:
header("Location: http://www.phpfreaks.com");
but it gives me an error..

#8 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 31 August 2006 - 12:51 AM

Yeah... more suited to javascript. We have a javascript forum specifically for such questions.

As for your error. I assume its the common headers already sent? See here.

#9 Revos

Revos
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 31 August 2006 - 12:58 AM

Ok, thank you I asked there.
About the headers problem this is exact problem I got:
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\Projects\a.php:13) in C:\wamp\www\Projects\a.php on line 26

At line 13 I have an HTML code and at line 26 is the code of the header..
Is there any other way exept header?

#10 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 31 August 2006 - 01:03 AM

At line 13 I have an HTML code and at line 26 is the code of the header..
Is there any other way exept header?


Post your code.

#11 Revos

Revos
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 31 August 2006 - 01:10 AM

User name:
<input name="User" type="text" /> <br />
Password:
<input name="Pass" type="password" maxlength="10" /> <br />
<input name="Send" type="submit" value="Login =]" />
</form>
<?php 
if (isset($_POST["Pass"])) 
{
 $Pass = $_POST["Pass"];
 $User = $_POST["User"];

 if($Pass == "bla" and $User == "bla2")
 {
 echo "Successfuly connected!<br>";
 header("Location: http://www.phpfreaks.com"); 
 }
...
?>
from line 10 untill line 27.

#12 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 31 August 2006 - 01:19 AM

Ok... what you need do is wrap your html within the if{} so that it is only displayed when needed. eg;

<?php 
if (isset($_POST["Pass"])) 
{
 $Pass = $_POST["Pass"];
 $User = $_POST["User"];
 if($Pass == "bla" and $User == "bla2")
 {
   header("Location: http://www.phpfreaks.com"); 
 }
} else {
  echo "
    User name:
    <input name=\"User\" type=\"text\" /> <br />
    Password:
    <input name=\"Pass\" type=\"password\" maxlength=\"10\" /> <br />
    <input name=\"Send\" type=\"submit\" value=\"Login =]\" />
    </form>";
}
?>

Also note that there is no need to try and display the Successfuly connected! as it will not be displayed prior to a redirect and will only cause you grief.

#13 Revos

Revos
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 31 August 2006 - 01:25 AM

Thank you very much!
oh and if you are making something huge that is a hell of a work...

#14 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 31 August 2006 - 01:28 AM

oh and if you are making something huge that is a hell of a work...


Not sure what you mean exactly... just to let you know though, your html does not need to be embedded within php, thats just my prfered method. This would be just as effective.


<?php 
if (isset($_POST["Pass"])) 
{
 $Pass = $_POST["Pass"];
 $User = $_POST["User"];
 if($Pass == "bla" and $User == "bla2")
 {
   header("Location: http://www.phpfreaks.com"); 
 }
} else {
?>
User name:
<input name="User" type="text" /> <br />
Password:
<input name="Pass" type="password" maxlength="10" /> <br />
<input name="Send" type="submit" value="Login =]" />
</form>";
<?php
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users