Page 1 of 1

help on putting pages together using isset and PHP_SELF plz

Posted: Mon Jul 22, 2002 12:53 pm
by pb2ya
ok, somehow ppl make 2 php pages into 1 using isset and php self. i want these 3 pages together, and if some1 could just tell me how, ill stop buggin u guys :P. anyways, PLZ help me.
postm.php-sends query to post.php

Code: Select all

<form method="post" action="post.php">
<h3>Type in your name:</h3><br />
<input type="text" name="username" /><br />
<h3>Type in your message:<br /></h3>
<textarea name="message"></textarea>
<br />
<input type="submit" value="Submit" name="submit" /><input type="reset" value="Reset" />
post.php-executes the query from postm.php

Code: Select all

<?php
$message = $HTTP_POST_VARS&#1111;"message"];
$username = "<b>" . $HTTP_POST_VARS&#1111;"username"] . "</b> on ";
$username .= date("g:i a, n/j/Y");
$conn = mysql_connect("localhost", "", "");
mysql_select_db("guestbook", $conn);
$query = "INSERT INTO posts (poster, message) VALUES ('$username', '$message')";
$result = mysql_query($query,$conn)or die("query failed");
?>
posts.php-shows the posts in the database.

Code: Select all

<?php
$conn = mysql_connect("localhost", "", "");
mysql_select_db("guestbook", $conn);
$query = "SELECT * FROM posts ORDER BY id DESC";
$result = mysql_query($query,$conn)or die("query failed");
?><p align="center">
<?php
while($r=mysql_fetch_array($result))
&#123;
    $postnum = $r&#1111;"ID"];
    $user = $r&#1111;"poster"];
    $message = $r&#1111;"message"];
    echo"<table border="3" width="40%"><tr><th>Post#</th><td>$postnum</td>
<th>Posted by:</th><td>$user</td></tr><tr>
<td colspan="4">$message</td></tr></table><br />";
&#125;
?>
PLZ help me or send me a tutorial on how to do this!

Posted: Mon Jul 22, 2002 3:06 pm
by BDKR
Let's see if we can understand you. Do you want one page to show a form, process a form, and possibly do other things as well? If that's the case, use a switch statment. Each time the form is submitted, the switch reads a var and decides which route to take.

Check into it at http://www.php.net/manual/en/control-st ... switch.php.

So if the page is called process_sprockets.php, each time a submit button is pressed, the page is being submitted back to process_sprockets.php. You could of course use the PHP_SELF constant, but if you know the name of the script, just put in the name of the script. Same difference and less code.

If after fighting with it a while you are still stumped, let me know.

later on,
BDKR

okok....

Posted: Mon Jul 22, 2002 10:33 pm
by pb2ya
ok, i read the stuff u put, and the stuff in my PHP book i have. ok, i understand switch, but how do i use switch to do what i asked?
ok, scratch that. for example, show me by posting a script that uses switch
to make it so that when u click a link, it changes a variable so there is a different case.

Posted: Tue Jul 23, 2002 2:02 am
by twigletmac
Have you read the examples in the manual? Have you tried them? Have you tried working from them to achieve what you want? Do you have a good reason why someone should write a script for you, without you making any attempt to help yourself?

Mac

Posted: Tue Jul 23, 2002 9:14 am
by BDKR
Well, I'm not going to write a script for you as I believe that would be doing you a dis-service, but I will give something to think about.

As you remember about a switch, there is one variable up at the top of the construct, and based on what it's value is, a matching case is chosen. That said, when you click a submit button on a form, the value passed with that submit button can be anything you want. Additionally, the variable associated wth that submit button can have the same name as the variable at the top of that switch. :!:

Just remember, one php page can have multiple forms, do multiple things, and so on and so on. But each time a script is executed, I don't HAVE TO do any of them.

Keep pluggin' away. You'll get it.
BDKR

...

Posted: Tue Jul 23, 2002 9:19 pm
by pb2ya
no, i tried it already but now im jsut trying to make a simple script so theres a link, and when you click it, the state changes so a variable is displayed, like this:

Code: Select all

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP page</title>
</head>
<body>
<?php
switch ($i) 
&#123;
case 0:
echo '$i == FALSE';
break;
case 1:
echo '$i == TRUE';
break;
&#125;
echo "<br /><a href="$PHP_SELF?i=1">click here to assign the value 1 to the variable I.</a>
?>
</body>
</html>
but this dosent work. if i can have this example work, ill be set. thanx guys 4 tryin to teach me, but ive been learnin php for a few months now and all i really understand are arrays, associative arrays, and mySQL.

Posted: Wed Jul 24, 2002 1:48 am
by twigletmac
You using PHP 4.2 or up, if so then I'm assuming you read this:
http://www.devnetwork.net/forums/viewtopic.php?t=511

Mac

Posted: Wed Jul 24, 2002 10:33 am
by BDKR
I think Twig is right. It looks very close otherwise.

That said, here is a snippet of just how a switch can be used in a web form / page. You are close enough now that this may get you over the hump.

Code: Select all

# May as well start the switch statement here
switch($HTTP_POST_VARS&#1111;search_parts])
	&#123;
	case "Begin Search":	// Start the search. Agent creation will be started here. 
		&#123;
		
		# Check fields
		if(check_fields_here($HTTP_POST_VARS, $required_variables) == 0)
		    &#123;
		    $exit_stat = 1;
			missing_info_message();
		    show_form($HTTP_POST_VARS, $HTTP_GET_VARS, $exit_stat);
		    &#125;
		else
		    &#123;
		    # Query the db and show results if there are any.
				# If there are aren't, show_parts() will call the 
				# concieve agent function, provided the user wishes to create an agent
				show_parts(build_query($HTTP_POST_VARS), $HTTP_POST_VARS);
		    &#125;
		break;
		&#125;

	case "Reset Form":	// Can you guess
		&#123;
		unset($HTTP_POST_VARS);
		show_form($HTTP_POST_VARS, $HTTP_GET_VARS, $exit_stat);
		break;
		&#125;

	case "Try Search Again":		# In order to try another search
		&#123;
		show_form($HTTP_POST_VARS, $HTTP_GET_VARS, $exit_stat);
		break;
		&#125;

	case "Try New Agent Name":		# For trying to create an agent after a failed attempt
		&#123;
		concieve_agent2($HTTP_POST_VARS);
		break;
		&#125;


	case "":	// If the page is being shown fresh
		&#123;
		show_form($HTTP_POST_VARS, $HTTP_GET_VARS, $exit_stat);
		break;
		&#125;

	default:	// This is really just a catch all. Execution should never get here.
		&#123;
		show_form($HTTP_POST_VARS, $HTTP_GET_VARS, $exit_stat);
		&#125;
	&#125;
# Program (er... ah... the script) should be done with execution at this point
Now the gist of this is that there is a button on all the forms called search_parts. Now search_parts could equal many different things based on the output of the program. If search_parts is empty or not set, then the script just displays a search form. If search_parts = 'Begin Search', then something else is done. The last thing you have to remember that search_parts in this case is a form button that's being passed using the post method. I'm also using the depecated $HTTP_POST_VARS[] stuff, but it works and all is good.

And of this on one page just as you asked.

I had prolly better state that all of the functions that are called in the above switch are either included from a function lib or written further down on the page (that isn't show here). That's why at the bottom it says....

Code: Select all

# Program (er... ah... the script) should be done with execution at this point
Hope this helps,
BDKR

uhh

Posted: Wed Jul 24, 2002 4:58 pm
by pb2ya
ok, i get it i think.

Posted: Wed Jul 24, 2002 5:06 pm
by BDKR
Right on!