Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get a freetext entry from a textbox, then pass it to a php href 1

Status
Not open for further replies.

svar

Programmer
Aug 12, 2001
349
0
0
GR
Basically the idea is to have a table and the user selects an entry from the table and clicks on an href, where 3 parameters are passed, of 2 different kinds: A) parameters from an SQL query from the loop that generates the table(e.g. $parameter1 and 2) and B) a free text input from a textbox. The issue is how to effectively pass the freetext to the href'd script.

I have seen some javascript solutions, not sure how one would do it with php or if it is possible.

Code:
<table>
<tr>
...

    <td><?php echo htmlspecialchars($parameter1);?></td>
    <td><?php echo htmlspecialchars($parameter2);?></td>
  <td><div align="center">       <input CLASS="textbox" type="text" name ="myfreetext" size=15>
                                                                                  </div></td>
 <td> <a href="select.php?key1=<?php echo $parameter1; ?>&key2=<?php echo $paramete2;?>">Go to this link</a> </td>
  </tr>
</table>
 
there are a number of ways

1. javascript. interrupt the click event on the link and add the values to the url.
2. change the fields into a form (<form> ... </form>) and change the link into a submit button.
3. as for (2) but leave the link as an <a>. Then interrupt the event and submit the form using javascript

i suggest (2) as it will work independently of whether javascript is enabled on the client browser. Consider also using POST rather than GET as the <form method=""> as some browsers have restrictions on the character count of a url. Unlikely to be a problem for a simple textbox, but possible if you migrate to a textarea.
 
there are a number of ways

1. javascript. interrupt the click event on the link and add the values to the url.
2. change the fields into a form (<form> ... </form>) and change the link into a submit button.
3. as for (2) but leave the link as an <a>. Then interrupt the event and submit the form using javascript

i suggest (2) as it will work independently of whether javascript is enabled on the client browser. Consider also using POST rather than GET as the <form method=""> as some browsers have restrictions on the character count of a url. Unlikely to be a problem for a simple textbox, but possible if you migrate to a textarea.
 
So something like this:
Code:
<?php
 if !(isset($_POST['freetext']))
{
    $_SESSION['freetext']=$_POST['freetext'];
}
?>

<table>
<tr>
...

    <td><?php echo htmlspecialchars($parameter1);?></td>
    <td><?php echo htmlspecialchars($parameter2);?></td>
<form action="select.php?par1=<?php echo $parameter1; ?>&par2=<?php echo $parameter2;?>" method="post">
<input type="submit" name="freetext" value=" " />
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Select">
</form>
  </tr>
</table>

This I assume will pass the sql query results $parameter1 and 2 and the freetext will be passed as a $_SESSION variable
Not sure if freetext could be passed in the same way as $parameter1 and 2 or whether this would even be desirable.
 
Why bother? Just put hidden fields in to the form with the values of your parameter 1 and 2.
 

If I understand you correctly,

Code:
/*don't need that:
<?php
 if !(isset($_POST['freetext']))
{
    $_SESSION['freetext']=$_POST['freetext'];
}
?>
*/
<table>
<tr>
...

    <td><?php echo htmlspecialchars($parameter1);?></td>
    <td><?php echo htmlspecialchars($parameter2);?></td>
<form action="select.php" method="post">
    <input type="hidden" name="par1" value="<?php echo $parameter1; ?>">  
    <input type="hidden" name="par2" value="<?php echo $parameter2; ?>"> 
//<form action="select.php?par1=<?php echo $parameter1; ?>&par2=<?php echo $parameter2;?>" method="post">
<input type="submit" name="freetext" value=" " />
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Select">
</form>
  </tr>
</table>
Then in select.php:
Code:
<?php
    $parameter1 = $_POST['par1'];    
$parameter2 = $_POST['par2'];
$freetext=$_POST['freetext'];// and get rid of the if isset and $_SESSION thing?
   ......
?>
 
there are some errors in your html. try this

Code:
<table>
    <tr>
        <td><?php echo htmlspecialchars($parameter1);?></td>
        <td><?php echo htmlspecialchars($parameter2);?></td>
        <td>
            <form action="select.php" method="post">
                <input type="hidden" name="par1" value="<?php echo $parameter1; ?>" />  
                <input type="hidden" name="par2" value="<?php echo $parameter2; ?>" /> 
                <input type="text" name="freetext" value="" />
                <input type="submit" name="submit1" value="Select" />
            </form>
        </td>
  </tr>
</table>

note also that including inputs directly within the form tags is not semantically correct. technically they should be within a block element such as a div or a fieldset. however i have never known this to affect functionality.
 
Yes, this worked very well. Thanks. I assume abuse of superglobals is kind of frowned upon much like the abuse of COMMON blocks in FORTRAN, hence somehow passing arguments via POST/GET methods is preferable?
 
Not sure what you mean by abuse.
It is fine to provide a superglobal value as ana argument or to access it directly. It is considered poor style to set a superglobal value manually (other than $GLOBALS) or to pass by reference when the value might be changed by the receiving function or method. But it is not disallowed programmatically just frowned on.
 
what do you mean "set manually?"
And, of course the point of passing variables is to make them known to the called program. If they are known anyway, there is little point in passing them. Do I understand you correctly?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top