homepage Welcome to WebmasterWorld Guest from 54.226.168.96
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Reading variables inside Smarty templates
toplisek




msg:4376449
 9:13 am on Oct 19, 2011 (gmt 0)

1. I have tpl files and need to get variables values from on page to another. Is there some correct way to do this or controller file should be on both pages.

Possibility is also GET function where needs to be read this variable(s):
foreach($_GET as $key=> $value){
echo '$_GET[' . $key . '] = ' . $value . '\r\n';
}

What should be placed in tpl and what inside PHP (controller files)

2. Reading page name within Smarty platform
Is there any solution to read page name with this platform or there is a way like:
$searchURL = $_SERVER['REQUEST_URI'];
echo '<br /><br />'.$searchURL;

$searchterm = $_SERVER['SCRIPT_FILENAME'];
echo '<br /><br />'.$searchterm;
//includes FOLDERS and FILE EXTENSIONS

function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}

echo '<br /><br />'.'The current page name is '.curPageName();

 

andrewsmd




msg:4376770
 9:15 pm on Oct 19, 2011 (gmt 0)

I don't really understand your exact issue but there are numerous ways you can pass variables across pages. One is to use get data like you mentioned. I.e. say you have
$var = "myValue";
You can redirect from your page to the second page and put it on the end like page2.php?=myValue. However, if you have any special characters in there you will need to encode it.

An easier approach would be to use a session or cookie variable.
I.e. in page1.php set
$_SESSION["var"] = "myValue";

Then in page2.php you can call $_SESSION["var"]; and it should output myValue. If you use sessions MAKE SURE you place the session_start(); method at the top of each page BEFORE any other code.

Using a cookie is just about as easy, but some people don't have cookies enabled, or security settings are too high. You can throw in checks for that though, and display a message if they don't. Try out some of the things I mentioned and post back if you have issues.

toplisek




msg:4377000
 9:56 am on Oct 20, 2011 (gmt 0)

if ($_GET["var"]=="fgtbGhLeChlA8IuS8I3RLatv")
{
$_SESSION["receiver"] = 'info@mydomain';
echo $_GET["var"];
}
GET var will not be detected in redirected page and of course session variable will not be set to specific receiver. what can be issue with this code that GET var will not be detected? Is this issue with Smarty and controller file as this code is inside controller file at the top? Sessions start function is defined.

andrewsmd




msg:4377054
 2:00 pm on Oct 20, 2011 (gmt 0)

You don't need to use get and a session var. Just use one or the other. By "not be set to the specific receiver" what do you mean? A session is specific based on the browser that is open on your site. Try a simple test, no smarty, to see if it's working.
create page1.php
<?php session_start();
$_SESSION['var'] = "myValue";
?>

page2.php
<?php session_start();
echo($_SESSION['var']);
?>
Can you get that to work?

toplisek




msg:4377538
 7:41 am on Oct 21, 2011 (gmt 0)

If I have links to ONE contact form, I need to push value of receiver to the GET function and than store into session variable if it is the same as GET ID. Than I transfer to contact form and send e-mail to specific receiver.

Your session works. Issue is how to send to specific receiver.

andrewsmd




msg:4377621
 1:03 pm on Oct 21, 2011 (gmt 0)

What do you mean by receiver? You mean the values from the input of the form? I'm not following you here. Give me a use case example of what it is you are trying to do and I might be able to help better.

toplisek




msg:4377777
 6:57 pm on Oct 21, 2011 (gmt 0)

ok, sorry not to be clear till now:

1. Link with Contact Us inside one page1:
<p><a href="contact_form.php?var=">email of contact number 1</a>--></p>
<p><a href="contact_form.php?var=">email of contact number 2</a>--></p>

2. Receiver is e-mail receiver, actually TO:
I have to use GET ID to receive ID and compare to actually e-mail address and than store SESSION variable to perform form with e-mail address like TO:

Let me know if this was more clear. If I understand I can not store SESSION variable after click without GET ID.

andrewsmd




msg:4377789
 7:18 pm on Oct 21, 2011 (gmt 0)

You can store a session variable after that. Once you set a session variable, it doesn't go away until the browser closes, or it expires due to inactivity, which you can set. Look at these examples and see if they help you. The session.php files are showing a session and the get.php files are showing get data. With the get files, look at the url of the second page. You wall to load the pages Session.php and Get.php respectively. Hopefully this will help bud.


Create a file called Session.php and put this in there
<?php session_start(); ?>
<html>
<body>
<form method="post">
<input type="submit" name="submit" value="Click me to set a session variable" id="submit">
<br />
<?php
if(isset($_POST['submit'])){
$_SESSION['myVar'] = "my value";
echo("You set the session variable, click <a href=\"Session2.php\">here</a> to see it.");
}//if
?>
</form>
</body>
</html>



Create a file called Session2.php and put this in there
<?php session_start(); ?>
<html>
<body>
<?php
if($_SESSION['myVar']){
echo("we set it, see it equals \"" . $_SESSION['myVar']. "\"");
}//if
else{
echo("couldn't find the session variable");
}
?>
</body>
</html>

Create a file called Get.php and put this in there
<html>
<body>
<form method="post">
Are you awesome?<br />
<a href="Get2.php?awesome=yes">Yes I am</a><br />
<a href="Get2.php?awesome=no">No I am not</a><br />
</form>
</body>
</html>


Create a file called Get2.php and put this in there
<html>
<body>
<form method="post">
<?php

if($_GET['awesome'] == "yes"){
echo("That's great, I'm awesome too!");
}//if
else{
echo("It's ok, I still like you. Even if you aren't awesome");
}//else

?>
</form>
</body>
</html>

toplisek




msg:4378309
 1:29 pm on Oct 23, 2011 (gmt 0)

Thank you this works. I like to use links and tried to do link NOT input button:
<form name="receiveremail1" action="session2.php" method="POST">
<a href="session2.php" onclick="document.receiveremail1.submit()">e-mail receiver</a>

Why is not stored session? Is this mistake?
couldn't find the session variable...

andrewsmd




msg:4378641
 1:10 pm on Oct 24, 2011 (gmt 0)

Because a link is a client side request. You aren't doing anything when they click that link. PHP has to have some trigger to make it run. That's why I used a button. The only way to do php on a link click would be to use AJAX. Judging from your level of difficulty with this, I would recommend you stay away from that. If you want to use a link and not a button, then you need to pass it to a page with get data. Try these

session.php
<?php session_start(); ?>
<html>
<body>
<form method="post">

<a href="session2.php?setVar=true">Click here to set it</a>
<?php

//we check to see if the session variable has been set
//if it has, then we will output it.
//note that this will only be set if they click on the link above
if(isset($_SESSION['myVar'])){

echo("<br />We set the session variable it is this \"{$_SESSION['myVar']}\".");
}//if
?>
</form>
</body>
</html>

session2.php

<?php session_start();

//if we came from session.php and it's telling
//us to set the session var in the get data
if($_GET['setVar'] == "true"){
$_SESSION['myVar'] = "My Value";

//no we redirect back to the session page
header("Location: session.php");
}//if
?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved