Jump to content
wdv

Simple Php Question From A Rookie

Recommended Posts

Simple question, but I've been banging my head on the keyboard for hours.

 

I need to pass a value to a form in php script. The HTML file that calls the script has the following line:

<A HREF="catapp1.php?nop=Abby">Adopt Abby</A>

 

Within catapp1.php, I want to grab the value out of nop (if any) as the default entry for an html form field.

 

The form works well for manual entry, but I can't find a way to pull the variable in as a default. The form is initialized as follows:

<form action="ufm/mailit.php" method="post"

enctype="multipart/form-data" name="form1">

 

Here's a failed example of where I try to pull the value of nop into the form as the default entry, but all I get in the field is the text "$_GET['nop']":

<b>Name of Pet: <INPUT name="name_of_pet"

value=$_GET['nop']

style="HEIGHT: 22px; WIDTH: 310px">

 

Can someone spell this out in simple terms for a php/script n00b? Thx.

 

Bill

Share this post


Link to post
Share on other sites

Hi wdv, Welcome to the forums,

There are two files you would need to modify

The first is the actual file that contains the Form

><form method="POST" action="ufm/mailit.php">
<p>Name of Pet:<input type="text" name= "NOP"></p>
<input type="submit" name="submit" value="SendInfo!">

 

 

The second is the ufm/mailit.php file

 

where you will wont to display the Name of the pet passed from the previous form

><?php 
echo "The name of the Pet is      ..................... ";
 echo"($NOP)<br>";
?>

 

 

The variable $NOP is assigned within the First bit of code where it says"

 

This is then available for use in the Called page

 

Hope this helps you

and Welcome aboard :)

I hope this helps

Share this post


Link to post
Share on other sites

Thanks for the coding suggestion and the quick responses from the welcoming committee. We're trying to finish up our TCH site so that we can transfer our domain (www.pawsct.org) before our current hosting agreement expires on Thursday.

 

I don't think I explained my problem clearly, so I put together two simplified samples to see this helps:

 

rover.html

<HTML><BODY>

<A HREF="dogapp1.php?name_of_pet=Rover">

Click to adopt Rover</A>

</BODY></HTML>

 

dogapp1.php

<HTML><BODY>

<form method="post" name="form1">

<p>

Name of Pet: <INPUT name="name_of_pet"><BR>

Your name: <INPUT name="your_name"><BR>

Your address: <INPUT name="your_address"><BR>

</p></form></BODY></HTML>

 

I need the "Rover" value passed by rover.html to show as the default value for name_of_pet in dogapp1.php, while allowing the user to modify the value and fill in the rest of the form. I can do this manually by adding value="Rover" to the name_of_pet INPUT field, but can't pull the variable value in correctly.

 

I played with PHP commands (e.g., $_GET, $_POST, $_REQUEST) but can't get the syntax right. Can someone point me in the right direction to modify dogapp1.php to get Rover (or Fido or Fluffy ...) as the default? Thanks!!!

 

-- Bill

Share this post


Link to post
Share on other sites

The problem with what you posted is that in your PHP file you don't mark off the part that is PHP code. This is done by enclosing the PHP code in tags like this:

<? ?>

or better yet, like this:

<?php ?>

 

Here is what I think you want to put in place of the code you posted:

 

><b>Name of Pet: <INPUT name="name_of_pet"
value="<?php = $_GET['nop'] ?>"
style="HEIGHT: 22px; WIDTH: 310px">

 

Basically, the way I do it is to create the form as I normally would, leaving the "value" blank (value="") and then come back afterward and insert the php code inside the quotes -- that's the way I keep it straight in my head.

Share this post


Link to post
Share on other sites
The problem with what you posted is that in your PHP file you don't mark off the part that is PHP code ...

 

Thanks. That's what I initially thought, but I can't seem to get the syntax right.

 

When I cut and paste from your example

>value="<?php = $_GET['nop'] ?>"

 

I get the following error:

Parse error: parse error, unexpected '=' in dogapp1.php on line 5

 

If I take out the equal sign, the script runs, but I get a blank field (no "Rover"):

>value="<?php $_GET['nop'] ?>"

 

I know it must be something simple that I'm missing ...

 

-- Bill

Share this post


Link to post
Share on other sites

I think you're going to alter matman's code a little, because it isn't telling the PHP to actually do anything with the $_GET value. And it needs a semi-colon:

 

 

><b>Name of Pet: <INPUT name="name_of_pet"
value="<?php echo $_GET['nop']; ?>"
style="HEIGHT: 22px; WIDTH: 310px">

Share this post


Link to post
Share on other sites
I think you're going to alter matman's code a little, because it isn't telling the PHP to actually do anything with the $_GET value. And it needs a semi-colon

:clapping:

Perfect, owatagal -- worked like a charm. I tried dozens of different syntax combinations, but none worked until yours.

 

All the dogs and cats at the shelter appreciate everyone's incredibly quick assistance!

 

-- Bill

Share this post


Link to post
Share on other sites

Hi Bill, and welcome to the family! B)

 

I'm glad to see you managed to get the script to work :clapping:

 

If you need anything, just ask! :)

Share this post


Link to post
Share on other sites

Actually, my code just had an extra space in it, and I should have used the short opening tag like so:

 

<?=$_GET['nop']?>"

 

And it doesn't need a semicolon (to run) since it's just the one line.

 

See the bottom of this manual page: http://us4.php.net/echo

Share this post


Link to post
Share on other sites
Actually, my code just had an extra space in it, and I should have used the short opening tag like so:

 

<?=$_GET['nop']?>"

 

And it doesn't need a semicolon (to run) since it's just the one line.

 

See the bottom of this manual page: http://us4.php.net/echo

 

Interesting & good to know. That will come in handy elsewhere, I think.

Share this post


Link to post
Share on other sites

owatagal and matman, note that short open tag (just <? instead of <?php ) may not work on some hosts (they may have it disabled), so if you intend to create a script for others to use, you may want to take that in consideration.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...