Jump to content

How to $_REQUEST a variable $var from another page


Qopzeep

Recommended Posts

Hello all,

 

I'm taking my very first steps into PHP Database scripting, and as an exercise I was trying to make a small CMS. It consists of the following:

 

Login page-->CMS Home-->Create new entry

[/td]\/

 

This is the code I'm struggling with on the CMS home page:

(this CMS is only intended as an exercise, therefore I left the die() statement in there.)

 

// Opening a connection to the MySQL server
$con = mysql_connect("localhost","<username>","<pword>");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Selecting the appropriate database
mysql_select_db("db_testcms", $con);

 

If user provided the correct login data, proceed with the script. The Table named Content has two columns, ContentName and ContentContents. Write the ContentName values to an array named $row. Also echo an edit button with the name=$row['ContentName'].

 

	$entries = mysql_query("SELECT ContentName FROM Content");

	while($row = mysql_fetch_array($entries))
	{
		echo $row['ContentName'];
		echo " <form action='cms-edit.php' method='post'>
       <input type='submit' name='" . $row['ContentName'] . "' value='Edit' /></form>";
	}

 

This outputs the name of the content and displays an edit-button next to it. When you click the edit button, you are taken to another page called cms-edit.php, where I would like to $_REQUEST the data.

 

The problem is that I used the $row['ContentName'] to name the button. So if I want to request the data on the edit page, I would like to do this: $_REQUEST['$row['ContentName']]. Of course I can't do this, but I'm sure there must be a way to accomplish this. Right  :-\ ?

 

So to sum it all up:

1- I would like to display the entries in a db, and an edit button next to it.

2- When you click the edit button, the name of that button is requested on the edit page, and the appropriate data is selected from the table.

 

I hope I have been clear enough. If I haven't please let me know, I will try to clarify everything. Thanks in advance,

Qopzeep

Link to comment
Share on other sites

The following will add a hidden field to each form with the content name as a value so that you can get it from the post data;

 

<?php
$entries = mysql_query("SELECT ContentName FROM Content");

while($row = mysql_fetch_array($entries))
{
echo $row['ContentName'];
echo '<form action="cms-edit.php" method="post">
	<input type="hidden" name="content_name" value="' . $row['ContentName'] . '" />
	<input type="submit" name="submit" value="Edit" />
</form>';
}

 

On cms-edit.php you can get the right info as follows;

 

<?php
$content_name = isset($_POST['content_name']) ? mysql_real_escape_string($_POST['content_name']) : NULL;
if(is_null($content_name)) {
echo "no content name was passed";
//you may want to redirect the user hear using header("Location: home.php");
exit;
}
$entries = mysql_query("SELECT `ContentName`, `ContentContents` FROM `Content` WHERE `ContentName`='$content_name'");

$row = mysql_fetch_array($entries);

//here you can echo a form with all the data gatehred about the content

Link to comment
Share on other sites

So to sum it all up:

1- I would like to display the entries in a db, and an edit button next to it.

2- When you click the edit button, the name of that button is requested on the edit page, and the appropriate data is selected from the table.

 

I hope I have been clear enough. If I haven't please let me know, I will try to clarify everything. Thanks in advance,

Qopzeep

 

it used to be realy awkward to do this and you had to set up all sorts of links etc for directional sort paging and all sorts, nowe we have frameworks that reqest JSON or XML from your script so basicaly you have a page that outputs the rusults from a query into XML or JSON and the javascript compoennt from the framework creates teh grid and has a button wich sends a SORT DIRECTION, SEARCH or PAGE START LIMIT string with the post when you click it and revieves the data again this time filtered by the POST.

 

Application nowerdays run off XML and JSON scripting is used to refine things , data griding and feeding is JSON

 

you need a grid ? that searches sorts and pages ?

 

dl extjs in js

 

instantiate a grid panel

instantiate a searchbar

instantiate a store (json or xml) (give it teh url of the php page you wish to output the plain text JSON or XML)

instantiate a paging toolbar

 

add the store to the toolbar

add the store to the grid

add the store to the search

 

insert a paging toolbar into the gridpanel

insert a search into the gridpanel

 

in your php script when you revieve posted data from teh components just filter your sql with it.

 

http://extjs.com/

http://extjs.com/deploy/dev/examples/samples.html

http://extjs.com/deploy/dev/examples/grid/edit-grid.html

http://extjs.com/deploy/dev/examples/grid/paging.html

http://extjs.com/deploy/dev/examples/form/custom.html

Link to comment
Share on other sites

Gevan's solution worked! :D Thanks very much!!  :P

 

There is still one tiny issue, when this codes executes:

 

<?php
...
while($row = mysql_fetch_array($entries))
	{
		echo $row['ContentName'];
		echo " <form action='cms-edit.php' method='post'>
		<input type='hidden' name='content_name' value='" . $row['ContentName'] . "' />
		<input type='submit' name='submit' value='Edit' />
		</form>";
	}

 

All the Edit buttons appear underneath the entry names  ??? I tried a css display: inline, but that didn't work.

 

Other than that small issue, it works fine now! Thanks again :]

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.