Jump to content

General questions...I dont know how to explain


Revos

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..
Link to comment
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.
Link to comment
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?
Link to comment
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..
Link to comment
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].
Link to comment
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?
Link to comment
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.
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.