Jump to content

Archived

This topic is now archived and is closed to further replies.

Revos

General questions...I dont know how to explain

Recommended Posts

this is my code:
[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>
[/code]
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..

Share this post


Link to post
Share on other sites
1/ You need to check the form has been submitted.

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

2/ Yoy can use the [url=http://php.net/header]header[/url]() function to redirect, or the [url=http://php.net/include]include[/url]() function to include a file.

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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  :)

Share this post


Link to post
Share on other sites
[quote]What do you mean is set?[/quote]

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?

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
Yeah... more suited to javascript. We have a [url=http://www.phpfreaks.com/forums/index.php/board,6.0.html]javascript[/url] forum specifically for such questions.

As for your error. I assume its the common [i]headers already sent[/i]? See [url=http://www.phpfreaks.com/forums/index.php/topic,95562.0.html]here[/url].

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote]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?[/quote]

Post your code.

Share this post


Link to post
Share on other sites
[code]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");
}
...
?>
[/code]
from line 10 untill line 27.

Share this post


Link to post
Share on other sites
Ok... what you need do is wrap your html within the if{} so that it is only displayed when needed. eg;

[code=php:0]
<?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>";
}
?>
[/code]

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

Share this post


Link to post
Share on other sites
Thank you very much!
oh and if you are making something huge that is a hell of a work...

Share this post


Link to post
Share on other sites
[quote]oh and if you are making something huge that is a hell of a work...[/quote]

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.

[code=php:0]

<?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
}
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.