Forum Moderators: open

Message Too Old, No Replies

Posting a variable to PHP

         

agent_davis

1:26 pm on Jan 27, 2011 (gmt 0)

10+ Year Member



Hi.

I am very new to javascript and html and desperately need some help to finish the code on my website.

A page on my site directs the user to a feedback page with a name appended to the url. I have managed to extract a name from the url in a java script on the feedback page.

I am now trying to post the name, described as the variable "firstlastname" to a php script that then emails me the information through the following hidden input tag, embedded in a form:

<form action="feedback.php" method="post">

<input type="hidden" name="teacher" value='<? php echo $firstlastname;?>' />
....

The problem is that I cannot seem to be able to link the name, which varies depending on a name clicked on the previous page (and shows up therefore in the url). I have tried a few different variations and seem to only get "firstlastname" showing up on the email, and not the actual name of the teacher. Ie., this is not a variable, only a fixed value. This is very annoying. The other part of the script, that directs the html page to show the name as separate first and last names, seems to be working, so I figure that there is a problem with the <input type...> line as shown above.

I have defined the value firstlastname above using the following line:

var firstlastname = delineate1(text)+' '+delineate2(text);

How can I fix this so that I get the value firstlastname?

Any help would be much appreciated.

rowtc2

2:16 pm on Jan 27, 2011 (gmt 0)

10+ Year Member



First thing, you can check what value is displayed on first page, before <form>...
Second, in page feedback.php you take variable this way:

$teacher = $_POST['teacher'];

Fotiman

2:18 pm on Jan 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!
It looks like you're trying to use PHP to set the value of the "teacher" input instead of using JavaScript, which is actually a better approach since not everyone has JavaScript enabled. If the page with this input field is a PHP page and the name has been passed into this page via the URL, then you should parse that in PHP. It would be something like this:

<?php
$firstlastname = $_GET["yourUrlParamName"];
?>


However, I'm not sure where the value is coming from. Do you have a bit more code that you could post that might be relevant?

agent_davis

12:14 am on Jan 28, 2011 (gmt 0)

10+ Year Member



Thanks for your responses guys.

@rowtc2: I have used that syntax in the php script to get the variable. I think the problem lies in the html (and php therein).

@Fotiman: Thanks for your suggestion.
What I have done in the feedback.html page is the following:

1st: define the variable firstlastname based on two functions from the url.

I then do a document write using firstlastname and this works as required (ie. the html page prints out the product of the functions as it should). Thus, I assume that there is no problem with the underlying function.
FYI - This line defines the variable firstlastname:

var firstlastname = delineate1(text)+' '+delineate2(text);

I then use a form to post various variables to the php script. Note, this is a html page (not a php page). I am under the impression that I can use php in an html page (but am not sure if this is right?!).

It looks like this:

<form action="feedback.php" method="post">

<tr><td width="115"><label for="tswname"><span class="Bold">*</span>Name</label>:</td><td width="361"><div align="left">
<input type="text" name="fullname" id="tswname" size="25" />

As you can see, the line above provides a space for the user to input their name. What I want to do is now also take the variable firstlastname (as defined earlier) and post it to the php script so that it will show up in the email.

I have been trying to use the following line to do this but I keep getting simply the code and not the variable in the field where the name should be:

<input type="hidden" name="teacher" value='<? php echo $firstlastname;?>' />

Hope this clarifies things.

Fotiman

2:31 am on Jan 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Note, this is a html page (not a php page). I am under the impression that I can use php in an html page (but am not sure if this is right?!).

No, regular html pages are not run through the php processor unless your server has been specially configured to handle them.

In addition, the variable in question here is not a PHP variable, it's a JavaScript variable. So even if the page was processed by the PHP, it wouldn't know anything about the $firstlastname variable.

Now, you could populate that hidden field using JavaScript by doing something like giving your input an id to match the name and then doing:

document.getElementById('teacher').value = firstlastname;

You would need to do that after the field existed in the DOM, for example, you could do this at the end of the document just before the closing </body> tag. However, as I said, this will end up being blank for users with JavaScript disabled.

jalicia18

9:33 am on Feb 4, 2011 (gmt 0)

10+ Year Member



<input type="hidden" name="teacher" value='<? php echo $firstlastname;?>' />

Try this one

<input type="hidden" name="teacher" value='<? php echo $firstlastname;?>' id="teacher" />


Javascript or Ajax

var teacher_value = "Test";
document.getElementById('teacher').value = teacher_value;