PDA

View Full Version : event action + php


weely
07-25-2006, 02:39 AM
Hello there,
pls I need help. I want to assign values to session variables when a link is clicked. But it does not seem to work. I have looked at similar threads (How to use $_POST) but it does not really answer my question.

Is there any other way to do this? please it urgent cos this is my uni project. Thanks

Here a snippet of the code (if it helps): {I have highlighted the problem area}

<?php
function get_list(){ //retrieve and displays results from the database
$conn = connect();
$date = set_date();
$list = "select a.agent_no, a.week_end, b.firstname, b.initial, b.surname from timesheet a, agent b where a.week_end = '$date' and a.staged_to = 'S' and a.agent_no = b.agent_no"; //include coy_code, location in query
$result = mysql_query($list, $conn);
echo mysql_num_rows($result)." Record(s) Found!";

echo "<table class=\"resultset\">";
echo "<tr><td>Name</td><td>Agent Number</td><td>Week Ending</td><td>Location</td><td>View Details</td></tr>";

$count = 0;
while($result_array = mysql_fetch_array($result)){
$agent_no = $result_array['agent_no'];
$week_end = $result_array['week_end'];
$firstname = $result_array['firstname'];
$initial = $result_array['initial'];
$surname = $result_array['surname'];
// $location = $result['location'];

if($count%2 != 0){ //to change background colour
echo "<tr class=\"switch\"><td>$firstname $initial $surname</td><td>$agent_no</td><td>$week_end</td><td>$location</td><td><a onclick=\"<?php set_globals($firstname, $surname, $agent_no, $date); ?>\" href=\"view.php\">View</a></td></tr>";
}
else{
echo "<tr><td>$firstname $initial $surname</td><td>$agent_no</td><td>$week_end</td><td>$location</td><td><a href=\"view.php\">View</a></td></tr>";
}
$count++;
}
echo "</table>";
}


function set_globals($a, $b, $c, $d){
$_SESSION[firstname] = $a;
$_SESSION[surname] = $b;
$_SESSION[agent_no] = $c;
$_SESSION[week_end] = $d;
}
?>

Fumigator
07-25-2006, 03:07 AM
PHP works on the server side, not the client side. You're trying to use PHP like it's Javascript.

What's a uni project by the way?

atadon
07-25-2006, 03:14 AM
i can think of 2 easy ways to solve this.

1. use get variables
2. use post variables

1. ur link would be view.php?fname=$a&sname=$b&agentno=$c&weekend=$d
then u can set your session vars in view.php using
$_SESSION['firstname'] = $_GET['fname'];
$_SESSION['surname'] = $_GET['sname'];
... and so on

2. u can use form with hidden fields such as
<form name="TESTFORM" action="view.php" method="post>
<input type="hidden" name="firstname" value="<?php echo "YOUR VAL HERE"?>"/>
<input type="hidden" name="surname" value="<?php echo "YOUR VAL HERE"?>"/>
......... etc etc
</form>

ur link would be as such
<a href="document.TESTFORM.submit()">link here</a>
or
<a href="#" onclick="document.TESTFORM.submit()">link here</a>

in view.php, u set ur session vars as such
$_SESSION['firstname'] = $_POST['firstname'];
$_SESSION['firstname'] = $_POST['surname'];


i'm not sure if there is a way to instantly set the session vars as u click on ur link, but those 2 options will surely do the job

hope it helps // cheers

atadon
07-25-2006, 03:15 AM
PHP works on the server side, not the client side. You're trying to use PHP like it's Javascript.

What's a uni project by the way?

uni was for university. i believe it's a north-american habit :cool:

weely
07-25-2006, 03:44 AM
Its a web-based timesheet management system

kaydara
07-25-2006, 09:26 AM
or you can add all your links to a table, and when each link is followed you pass smth like ?link=X , and when you recive a link in the url you can update the SESSION
if( isset($_SESSION["link1"]))
$_SESSION["link1"]++;
else
$_SESSION["link1"]++;

this on your session, or you table with a query like that:

this for clicks/users relation
$query = "INSERT INTO tbl_clicks VALUES(idlink,iduser,click=click+1)
or
this for link/clicks relation
$query = "INSERT INTO tbl_clicks VALUES(idlink,click=click+1)

as you see you have many options

weely
07-25-2006, 10:46 AM
Thanks a lot guys, I have tried using the get method, I can see the values is the URL but unfortunately I haven't been able to access them. as suggested by atardon, what I have done is:

In original script:
view.php?fname=$a&sname=$b&agentno=$c&weekend=$d

In view.php:
$a = $_GET['fname'];
echo $a;

this is still not working cos, the value of $a is not printed to the screen.
Am I doing something wrong? - thanks.

weely
07-25-2006, 10:48 AM
Thanks guys, it's now working. I should buy u drinks if u're nearby. I really appreciate it.

kaydara
07-25-2006, 03:01 PM
you havent tell us where you from yet!