Page 1 of 1

form handle

Posted: Sun Nov 20, 2005 11:38 pm
by spitfire_esquive
i'm not sure if this should be in this category, mods please feel free to move it to the appropriate category..with this being an "intermediate" topic....

i am able to search the database in mysql, retrieve the results and then dump them in an array (using mysql_fetch_array) and i have them displayed in a nice little table with links to them. one field is the unique identifier (which is also a link). ideally if you click on that link you would be pointed to a form with the unique id and the other variables present in an editable form.

my problem here is how to make an editable form, do you need a special "class" or start a "session" so that the variables of the unique identifier be passed on to the form fields?

an example is when you click on Profile link on this page to edit your profile. i'm sure i have a unique identifier (maybe my login) but it is not seen in the URL search string :

profile.php?mode=editprofile

when i arrive at the page itself i am able to edit my settings/preferences. then there's the submit button below that i presume updates my details by using an UPDATE mysql function.

many thanks! sorry for the meandering questions...that's the only thing holding me back right now in my project....

Posted: Mon Nov 21, 2005 1:40 am
by trukfixer
what you are looking for , is what we call "maintaining state" , and for this you can use sessions, cookies, POST , or GET .. my personal preference would be to use sessions, or POST as a second choice .. otherwise, $_GET an unique identifier could be stored in an input type="hidden" field, for example, or simply stored to the $_SESSION global ...

in order to *get* that unique identifier in the form, you need to send it from your page to teh next php file, which would pick it up, and then echo the value out into teh hidden form input's "value" field ..

hopefully this'll point you in the right direction enough to get some code laid out that you can post here, then we can nitpick at it and help you get it working .. but do be sure and read up a bit on teh subject at php.net :)

Posted: Mon Nov 21, 2005 6:08 am
by spitfire_esquive
thanks trukfixer :)

Posted: Mon Nov 21, 2005 6:18 am
by spitfire_esquive
sorry, last question about this subject....

i've decided to use sessions as i think it is tackled extensively in the php book i have now (makes for some very interesting bedtime reading:) )....if i am able to pass the 'id' field will the rest of the fields in the database be passed on to the form as well?

for example i have 3 fields, 'id','name','address'...if i was to be able to pass the value of 'id' through sessions will the fields 'name' and 'address' be passed to the form? do i pass this through :

Code: Select all

<input type="text" name="name" value="$name">
thank you thank you thank you thank you thank you

Posted: Mon Nov 21, 2005 8:57 am
by trukfixer
no if you want something passed in teh session data, you have to assign it to session data:

Code: Select all

<?php
//get post data into vars

$name = $_POST['name'];
$address = $_POST['address'];
$id = $_POST['id'];

//now you have variables with data from the form post..
session_start(); //actually it is much better to put this at the start of the file - I assume you have used it

$_SESSION['id'] = $id;
//OK now $id value will be passed through to subsequent pages and available in the $_SESSION superglobal
//however we must assign name and address to get those passed through also
$_SESSION['name'] = $name;
$_SESSION['address'] = $address;
//now we can use the $_SESSION variables in teh next pages we go to
?>
//assume this is the next page, say "repop_form.php"
<?php
//we're in a new page, start session 
session_start();
//OK we want to echo $name, $id, $address in form..
$name = $_SESSION['name'];
$address = $_SESSION['address'];
$id = $_SESSION['id'];

echo ('
<form action="whatever.php" method="post">
name: <input type="text" name="name" value="$name"><br>
');
//above an example of multi line echo of html getting $name in form field
//below an example of breaking out of php and using straight html
?>
addy: <input type="text" name="address" value="<?php echo $address; ?>"><br>

<?php
//or we can assign a variable to a template class such as smarty (see basic tutorial elsewhere) 
$smarty->assign('document_id',$id);
//and during template display, simply do like so: 
/*
<input type="hidden" name="id" value="{$document_id}">
</form>
/*
?>
note that if you do not assign a value into $_SESSION superglobal, the data will be lost to you next page *unless* you insert it into a form - you do *not* need to pass a value from session to form in order to pass data along, however.
You can go through 3 or 4 more pages without doing anything after $_SESSION['name'] = $name; , and several pages later when you need it, you can then do $name - $_SESSION['name']; and have that $name variable available again later.. no need to pass it in POST or COOKIE or GET from page to page :)

HTH.

Posted: Mon Nov 21, 2005 9:11 am
by spitfire_esquive
if you are in the philippines let me know i'll show you around and will let you sample the local "fare" ;)

Posted: Mon Nov 28, 2005 5:47 pm
by spitfire_esquive
ok, here's what i'm experimenting with, i've got 3 pages. the first page contains the form where there are two input boxes and a submit button that would point me to the 2nd page. 2nd page does the POST and the session and it displays the data that i just typed in. the 3rd page is an attempt to see if the session variables would appear. but unfortunately it doesn't :( i checked my php.ini and it seems to be OK with the settings. as far as i know register_globals should be turned off...

first page - ses1.php

Code: Select all

<form action="ses2.php" method="POST">
<html>
    <head><title>Session experiment</title></head>
    <body>

    <input type="text" name="first"><br>
    <input type="text" name="second"><br>
    <input type="submit" value="submit">

    </body>
</html>
second page - ses2.php

Code: Select all

<?php
    //create variables
    $first=$_POST['first'];
    $second=$_POST['second'];
    
    session_start();
    $_SESSION['first'] = $first;
    $_SESSION['second'] = $second;
    
    echo "$first,$second<br>";
    echo "<a href='http://localhost/ses3.php'>Page 3</a>";
?>
third page - ses3.php

Code: Select all

<?php
    session_start();
    
    $first=$_SESSION['first'];
    $second=$_SESSION['second'];
    
    echo "$first,$second";
    
?>
i'm in a rut right now, it seems that everything i try to use doens't work :( well i guess there would be better days. any help would be greatly appreciated!

Posted: Mon Nov 28, 2005 6:00 pm
by spitfire_esquive
FOUND IT!!!

in the php.ini file, the value should be a "" (blank). in my present php.ini the value for session.save_path was "/tmp"

here's some code

Posted: Tue Nov 29, 2005 1:36 am
by spitfire_esquive
here goes nothing..this code searches the database and then presents the results using an array and they are separated by tables. what i wanted to do with sessions was use their client names so that i can update/change their query details by using another form : sestest.php

gzuk.php

Code: Select all

<html>
    <head><title>Search Results</title></head>
    <body>
<?php
    session_start();
    
    //page header
    require('gcallheader.php');

    //create variable names
    $startd=$HTTP_POST_VARS['startd'];
    $endd=$HTTP_POST_VARS['endd'];

    //create a connection to the database
    $a=mysql_pconnect('----','---','---');
    
    if (!$a)
    {
      echo "Could not connect to MySQL.  Please try again later.";
      exit;
    }

    //select db
    mysql_select_db('globalcalls');
    
    //create the search string
    $query="SELECT * FROM globalcallsentry WHERE datecall BETWEEN '.$startd.' AND '.$endd.'";

    //execute the query
    $result=mysql_query($query);
    
    //check the number of affected rows
    
    $num_result=mysql_num_rows($result);

    if ($num_result == 0)
    {
      echo "<font color='red'><strong>No Queries</strong></font><br>";
      echo "Please click <a href='http://localhost/gsearch.htm'>here</a> to search again.";
      exit;
    }
    else
    {
      echo "There are $num_result queries.";
      //exit;
      
      //dump the results in an array
      for ($i=0; $i < $num_result; $i++)
      {
        $row=mysql_fetch_array($result);
        echo "<table border='5'>";
        echo '<tr><td><p><strong>'.($i+1). '.Date received:</t> ';
        echo htmlspecialchars($row['datecall']);
        echo '<td>Time Call:<br>';
        echo htmlspecialchars($row['timecall']);
        echo "<td>Client name:<br>";
        echo htmlspecialchars($row['clientname']);
        $nameentry=htmlspecialchars($row['clientname']);
        $_SESSION[htmlspecialchars($row['clientname'])]=$nameentry;
        echo "<br><a href='http://localhost/sestest.php'>Update</a>";
        echo '<td width="300">Nature:<br>';
        echo htmlspecialchars($row['probdesc']);
        echo '<td>Escalations:<br>';
        echo htmlspecialchars($row['escalations']);
        echo '<td>Action:<br>';
        echo htmlspecialchars($row['action']);
        echo '</tr>';
        echo '</table>';
        
      }
    }

?>
<body>
</html>
sestest.php

Code: Select all

<?php
    session_start();
    
     require('gcallheader.php');
    
    $nameentry=$_SESSION[htmlspecialchars($row['clientname'])];
    
    echo "$nameentry";
?>
i used echo to see if $nameentry would be passed on to sestest.php but unfortunately it is not. can you please tell me what i am doing wrong in terms of session_start placement and the $_SESSION function placement?