Forum Moderators: coopster

Message Too Old, No Replies

for as a PHP variable?

storing a form in the $content variable

         

nshack31

6:39 pm on Jan 2, 2005 (gmt 0)

10+ Year Member



hi I use the following code in each of my php pages...

<?php
$page_title = "Test";
$content = "This is where i enter my test that gets inserted to the default template";

include("default_template.php");
?>

I then call the $content in the default_template.php page.

It works good. But, the problem I am having is this.. I would like to add a login page and therefore need to add the following form..

<form method="post" action="">
Name : <input name="username" type="text"><br>
Password : <input name="password" type="password"><br>
<input name="send" type="submit" value="Send!">
</form>

I would like to store this form in the $content variable so that I can call it as usual on the default template page. IS there a way of doing this? Or will I have to stop using the default template page? please help!

willybfriendly

6:46 pm on Jan 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can either piece it together:

$content .= "<form method=\"post\" action=\"\">";
$content .= etc.....

(notice that quotes are escaped)

or, use heredoc syntax

$content =<<<EOF
<form method="post" action="">
Name : <input name="username" type="text"><br>
Password : <input name="password" type="password"><br>
<input name="send" type="submit" value="Send!">
</form>
EOF;

WBF

nshack31

6:52 pm on Jan 2, 2005 (gmt 0)

10+ Year Member



superb! I didn't know about using "\" before quotes. Thank you for your help

nshack31

6:57 pm on Jan 2, 2005 (gmt 0)

10+ Year Member



Hi I have another question to do with the same script. Sorry about this I'm new to PHP!

If I created a form in html I would use javascript to check that the user has entered a value

e.g.

<script language="Javascript"
type="text/javascript">
function ValidateForm() //create function called validate form
{
if (document.login.realname.value == "") //if password name = blank
{
alert("Please Enter a Name!"); //show error
document.login.realname.select(); //move cursor to field
return false; //go no further
}

else if (document.login.spearname.value == "") //if password name = blank
{
alert("Please Enter Your Spearhead Name!"); //show error
document.login.spearname.select(); //move cursor to field
return false; //go no further
}

And so on.

When using PHP is Javascript used? or do you use the action="<?php=$_SERVER['PHP_SELF'];?>"> command to print on the same page the error? if so, how is this done?

thanks again!

willybfriendly

7:51 pm on Jan 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can freely mix js and php. I am working on a script doing just that right now, though I may be posting to the js forum in a couple of minutes - I am pathetic at js.

So, I have this in my head:

function ConfirmChoice()
{
answer = confirm("This will delete all data relating to this item from the data base! This is irreversible. Are you sure you want to continue?")
if (answer!=0)
{
location = "<? echo $action;?>"
}
}

And this in my content var (which of course is in the body)

$content .= "<td><form action=\"$action\" method=\"post\" onSubmit=\" ConfirmChoice(); return false;\">";

WBF

mcibor

9:42 pm on Jan 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



nshack
your second code is allright! Better to use javascript alert for that, because you don't reload the page. Use php to confirm, that user has entered a proper value, like login and proper password.

You can mix php with javascript, however remember, that php is on the server side, while javascript is working on the client's side (even offline). So you can write javascript functions in php, but cannot call php functions in java.

That means that you can write:
<?PHP
print("<script>
<!--
document.write(/"Hello World/");
//-->
</script>");
?>

But you cannot write so:

<input type="text" id="1" name="1" onblur="document.getElementById(2).value = <?echo md5("this.value");?>">
<input type="hidden" id="2" name="2">

This won't work! because php is done before sending the page to user, so in the code you will get the following:

<input type="text" id="1" name="1"
onclur="document.getElementById(2).value = 6751kjhgdf9asf320987kl20950">
<input type="hidden" id="2" name="2">

where 6751kjhgdf9asf320987kl20950 is md5 hash of text "this.value", not of the value you write into the text field.

There's a way for that to work correctly:
When you submit the page, then the following page should have the code below:
<?PHP
$input2 = md5($_POST["1"]);
?>
Then you have md5 hash of what was written in <input name="1">

Have fun with php!
Michal Cibor

coopster

11:54 pm on Jan 2, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



And welcome to WebmasterWorld, nshack31!

nshack31

9:25 am on Jan 3, 2005 (gmt 0)

10+ Year Member



thankyou. Thanks for the help I've sorted that now. I'm sure I'll be posting more once I start working on the login systems etc!