By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

Having a problem bringing contents of a variable from one page to another.

P: 36
Hi

I am writing a php script that contains a bunch of links. Each link when clicked redirects to the same page however where the clicked link once was is now a textfield. The user then types into the text field once the user has entered input the page redirects to itself again this time where the text field was is a link again. The link is wutever the user typed into the previous text field. I am doing this by using $_REQUEST[....] to get wutever the user types in. So far Ive only been able to get one of these to work. The problem I am having is when the user clicks another link after the process is completed the previous process's variable is erased and the default link is outputted. Can someone help me with this problemF? I am posting a piece of my script.

Thanks

[PHP]$outer_link1 = "<a href=\"?Expand=outer_link1\" >outer_link1</a>";
if ($_REQUEST['Expand'] == "outer_link1")
{
$TextField1 = "<input type=text name=outer1 onChange='submit();' ";
echo("<td>$TextField1</td>");



}

else
{

$outer_link1_contents= $_REQUEST['outer1'];

if ($outer_link1_contents)
{
$outer_link1a = "<a href=\"?Expand=outer_link1\" >$outer_link1_contents</a>";
echo("<td>$outer_link1a</td>");

}
else
{
echo("<td>$outer_link1</td>");
}


}

$outer_link2 = "<a href=\"?Expand2=outer_link2\" >outer_link2</a>";



if ($_REQUEST['Expand2'] == "outer_link2")
{
$outer_link1_contents= $_REQUEST['outer1'];
if ($outer_link1_contents)
{
$outer_link1a = "<a href=\"?Expand=outer_link1\" >$outer_link1_contents</a>";
echo("<td>$outer_link1a</td>");


}
else
{
echo("<td>$outer_link1</td>");
}
$TextField2 = "<input type=text name=outer2 onChange='submit();' ";
echo("<td>$TextField2</td>");



}


else
{


$outer_link2_contents= $_GET['outer2'];
if ($outer_link2_contents)
{
$outer_link2a = "<a href=\"?Expand2=outer_link2\" >$outer_link2_contents</a>";
echo("$outer_link2a");
}
else
{
echo("<td>$outer_link2</td>");

}

} [/PHP]
Feb 14 '08 #1
Share this Question
Share on Google+
18 Replies


nathj
Expert 100+
P: 938
Hi,

Each time a user clicks a link they get a text box, completing the text box is supposed to change the text of the link. this works but only for one link at a time.

I think this is bacuase when you pist to a page it gets the page definition from the server and unless you change that file then you will always have this problem. what is happening is you are posting a vraibale to the target page and it's using it just fine. then with the second one it';s getting the original page as the file has not been updated.

To do what you want there are a couple of options open to you.

1. A database
Define the page based on a list of links in a table, then when a user makes a change to this the field in the table is updated and the page is redefined using the latest data from the database.

2. AJAX
When the user clicks a link simply display the text box without going to another page, then one ce the text is entered you can display the link again. This would be the same for each link and as it would never go and get the whole page from the server you would get teh desired results.

The deciding factor is really how permanent the change needs to be and should differnt users be able to set these things independantly of one another.

My prefered solution would be a combo of the database and AJAX.

Decide what exactly this system needs to do and for who and then pick a solution. Read up on the technology and post back here with any questions - I'm happy to try to help.

Cheers
nathj
Feb 14 '08 #2

P: 36
Hi,

Each time a user clicks a link they get a text box, completing the text box is supposed to change the text of the link. this works but only for one link at a time.

I think this is bacuase when you pist to a page it gets the page definition from the server and unless you change that file then you will always have this problem. what is happening is you are posting a vraibale to the target page and it's using it just fine. then with the second one it';s getting the original page as the file has not been updated.

To do what you want there are a couple of options open to you.

1. A database
Define the page based on a list of links in a table, then when a user makes a change to this the field in the table is updated and the page is redefined using the latest data from the database.

2. AJAX
When the user clicks a link simply display the text box without going to another page, then one ce the text is entered you can display the link again. This would be the same for each link and as it would never go and get the whole page from the server you would get teh desired results.

The deciding factor is really how permanent the change needs to be and should differnt users be able to set these things independantly of one another.

My prefered solution would be a combo of the database and AJAX.

Decide what exactly this system needs to do and for who and then pick a solution. Read up on the technology and post back here with any questions - I'm happy to try to help.

Cheers
nathj
^^^^^^^^^^^^^^^^^^^^^^
Unfortunatly Im limited in how I can work on this. I dont have direct access to the database. And Im not allowed to create my own tables. So thats out.

As for AJAX...I had this working in javascript. The problem I had with that tho was that php wasnt able to access wutever I put in the javascript textfield. So thats also out.

Is there any other way to do this or am I trying the impossible at this point?
Feb 14 '08 #3

nathj
Expert 100+
P: 938
^^^^^^^^^^^^^^^^^^^^^^
Unfortunatly Im limited in how I can work on this. I dont have direct access to the database. And Im not allowed to create my own tables. So thats out.

As for AJAX...I had this working in javascript. The problem I had with that tho was that php wasnt able to access wutever I put in the javascript textfield. So thats also out.

Is there any other way to do this or am I trying the impossible at this point?
Okay so those in charge are making life difficult for you. I think though that AJAX can be your solution as the text fields are still just html text fileds. when the link is clicked simply load the text fields then on the submit of the text field call a JS function with the ID of the text field the JS can get the value and using the XMLHTTP Request object it can be passed to php page with the value on the end of the query string to be retreived by $_GET[].

I think this is the best bet you've got.

Cheers
nathj
Feb 14 '08 #4

ronverdonk
Expert 2.5K+
P: 4,258
I must admit I haven't looked very carefully at the code (since it is not complete), but have you thought of using the $_SESSION array as a way of saving these links?

Ronald
Feb 14 '08 #5

P: 36
I must admit I haven't looked very carefully at the code (since it is not complete), but have you thought of using the $_SESSION array as a way of saving these links?

Ronald
I have no idea how to use SESSION. You got a link?
Feb 14 '08 #6

ronverdonk
Expert 2.5K+
P: 4,258
The $_SESSION array allows you to exchange data between pages. Works like an associative array. See these tutorials:
http://www.tizag.com/phpT/phpsessions.php
http://msconline.maconstate.edu/tuto...8/php08-01.php
http://www.phpdeveloper.org/news/5704

Ronald
Feb 14 '08 #7

P: 36
okay how do I use a session to retrieve the contents of a text field?

Would this work?

[PHP]

$TextField1 = "<input type=text name=outer1 onChange='submit();' >";

echo("$TextField1");

$_SESSION['outer1'] = $_REQUEST['outer1'];

echo("$_SESSION['outer1'] ");
[/PHP]
Feb 14 '08 #8

P: 36
^^^^^^^^^^^^^^^^

Yea that code piece dont work. The only thing I cant figure out is how to retrieve the user input in the text field and then store it in a session variable.
Feb 14 '08 #9

ronverdonk
Expert 2.5K+
P: 4,258
^^^^^^^^^^^^^^^^

Yea that code piece dont work. The only thing I cant figure out is how to retrieve the user input in the text field and then store it in a session variable.
When you want to use the $_SESSION array you MUST start the session at the very beginning of your script, like
[php]<?php
session_start();
.. rest of code
[/php]
In your code sample that would be:
[php]
<?php
session_start();
$TextField1 = "<input type=text name=outer1 onChange='submit();' >";

echo("$TextField1");

$_SESSION['outer1'] = $_REQUEST['outer1'];

echo $_SESSION['outer1'];[/php]
Observe that the echo statement has been corrected because the original one would not show any output.

Ronald
Feb 15 '08 #10

P: 36
okay I got the code to work. I still have the same problem that the value in the variable is being erased once I click a different link. Wut else can I do? Is there sumthing Im missing?

[PHP]

/* ########################### UNEXPAND ################################# */


$unexpand = "<a href=\"?Expand=Unexpand\" >Unexpand</a>";
echo("$unexpand");
session_start();
$outer_link1 = "<a href=\"?Expand=outer1\" >-0-</a>";
$TextField1 = "<input type=text name=outer1 onChange='submit();'>";
$_SESSION['outer1_value'] = $_REQUEST['outer1'];

if ($_REQUEST['Expand'] == "outer1")
{
//$textfield1_value = $_GET['outer1']
//value=$textfield1_value

echo("<td>$TextField1</td>");


}
elseif ( ($_REQUEST['Expand'] == "Unexpand")
|| ($_REQUEST['Expand'] !== "Unexpand") || ($_REQUEST['Expand'] !== "outer1"))
{


echo("should be a changed value");
if (!($_SESSION['outer1_value']))
{
$outer_link1 = "<a href=\"?Expand=outer1\" >-0-</a>";
echo("<td>$outer_link1</td>");
}

echo "<td>session value here<a href=\"?Expand=outer1\" >" . $_SESSION['outer1_value'] . "</td></a>";

}

else
{
echo("Do nothing");
}
[/PHP]
Feb 15 '08 #11

ronverdonk
Expert 2.5K+
P: 4,258
If you have a session_start() at the top of each script, you do the blanking out of the $_SESSION variable yourself.

Ronald
Feb 15 '08 #12

P: 36
If you have a session_start() at the top of each script, you do the blanking out of the $_SESSION variable yourself.

Ronald
where should I put it then?
Feb 15 '08 #13

ronverdonk
Expert 2.5K+
P: 4,258
where should I put it then?
'
Sorry for my not-too-clear post. What I mean is that, when you correctly issue the session_start() at the top of each script, then no data in that $_SESSION array can be lost between pages.
So a blank entry in the array is your own doing.

Sorry for the misunderstanding.

Ronald
Feb 15 '08 #14

P: 36
'
Sorry for my not-too-clear post. What I mean is that, when you correctly issue the session_start() at the top of each script, then no data in that $_SESSION array can be lost between pages.
So a blank entry in the array is your own doing.

Sorry for the misunderstanding.

Ronald
Its pretty much right at the top of the page. Im putting in the first few lines. Because the previous author of this script is using session variables for various db related processes. Should I destroy his old session before I start my own?
Theres the first few lines of my script
[PHP]
<?php
require("/scenarios/callable.php");
$db_conn = connect_db();
session_start();

.....

[/PHP]
Feb 15 '08 #15

ronverdonk
Expert 2.5K+
P: 4,258
You can leave the db-related vars in as long as you are sure that the 'require' script does not also start a session. That is why I would put in at the top right after the <?php statement.

Ronald
Feb 15 '08 #16

P: 36
You can leave the db-related vars in as long as you are sure that the 'require' script does not also start a session. That is why I would put in at the top right after the <?php statement.

Ronald
nah that didnt work. Any other suggestions? Cuz I really got no other ideas on wut to do at this point.
Feb 15 '08 #17

P: 36
nah that didnt work. Any other suggestions? Cuz I really got no other ideas on wut to do at this point.
Well I got it to work. Define the value of the session variable like this and add it to the rest of the code from the previous post

[PHP]$_SESSION['outer2_value'] = ($_REQUEST['outer2'])?$_REQUEST['outer2']:$_SESSION['outer2_value']; [/PHP]
Feb 29 '08 #18

ronverdonk
Expert 2.5K+
P: 4,258
Well I got it to work. Define the value of the session variable like this and add it to the rest of the code from the previous post

[PHP]$_SESSION['outer2_value'] = ($_REQUEST['outer2'])?$_REQUEST['outer2']:$_SESSION['outer2_value']; [/PHP]
Glad you, at last, found it yourself. See you next time.

Ronald
Feb 29 '08 #19

Post your reply

Sign in to post your reply or Sign up for a free account.