Jump to content

Created Variables / Now can't use them ...


lightsguy

Recommended Posts

Background: I know very little about PHP. I can usually take a script and modify it for my needs. In this case, I've been able to write the section of code below for my needs.

 

This is part of a form to mail script that people use to request ports on switches.

 

<? function request_row($limit){ 
  for ($x=1; $x<=$limit; $x++) 
  echo"<tr><td>Server Name: <input type=\"text\" size=\"20\" name=\"server$x\"></td>" .
"<td>Location: <input type=\"text\" size=\"20\" name=\"location$x\"></td>" .
"<td>Switch: <input type=\"text\" size=\"20\" name=\"switch$x\"></td>" .
"<td>Port: <input type=\"text\" size=\"20\" name=\"port$x\"></td></tr>" ;
}

 

Further down in my script, I have

request_row($howmany) ;

 

which calls the function above. ($howmany comes from another portion of the script.)

 

The problem I'm running in to, is that I don't know how to get this to print into an email. Now, I understand how to create the email portion, I'm not asking for help there.

 

function mailme() {
$to = "[email protected]" ;
$subject = "Subject";
$headers = 'From:' . $_POST["emp_email"] . "\r\n" .
   'Reply-To: [email protected]' . "\r\n" .
   'X-Mailer: PHP/' . phpversion();
$message =
$_POST["emp_name"] . " has filled in the form ..." ;

mail($to,$subject,$message,$headers) ;

 

What I need to do is get the results of those variables in "request_row" into the email.

 

I'm not asking for someone to give it to me. Just tell me where I need to start looking and I'll try and figure it out myself.

Link to comment
https://forums.phpfreaks.com/topic/37611-created-variables-now-cant-use-them/
Share on other sites

don't echo your result in your function instead save it as a varible in the function.

 

such as $answer='what you were echoing';

 

then at the end of the function put this

return $answer;

 

then when you call your function you would do something like this

 

$answer=request_row($limit);

then when you type you message you can enter $answer where ever you want and it will enter that bit of text or you can also echo $answer to the browser. You should never echo in functions as it can cause problems displaying html correctly. always return the result then echo that result

I'm new enough to know this ... I didn't ask my question well enough. My apologies for this.

 

I guess my question is this ...

 

I've created the variables, based on the number of requests the person is making. (server$x, location$x, in function request_row($limit))

 

Now, from my limited knowledge I know I can use these variables like this....

$server = $_POST['server'] - Then use $server in the email.

or

$message = "Server" . $_POST['server'] . ; //My syntax may be a hair off, but you get the idea.

 

Now, the confusing part to me is the fact the number of $server variables is going to vary each time.

 

If used (and I don't know if this is legal)

$message = "Server" . $_POST['server$x'] . ;

How would I tell PHP to increment $x? I figured it out in the function (see first post) but I can't figure out how to duplicate that feat here. I would need to repeat the entire row up to the number of times specified in $limit (again see first post.)

 

Again, if you can point me in the direction to look, I don't mind reading and learning.

 

Some progress, I think ...

 

If the user selects 3 requests, then the form will show the username, email, etc. (not shown on this portion of code.) However, when the script shows the server/location, etc., it only shows the LAST request. If I chose 3, it only shows the third. Pick 7, it only shows the last.

 

I'm sure it's something really simple, something that I should learn. If you can point me in the right direction I'll go research.

 

function show_row($limit) {
for ($x=1; $x<=$limit; $x++)
$answer=" Server name:" . $_POST["server$x"] . "Location" . $_POST["location$x"] . "Switch:" . $_POST["switch$x"] . "Port:" . $_POST["port$x"] . "\n\n" ;
return $answer;
}

 

Then ... (and the echo's are in this portion so I can see the results without mailing the form over and over. Would be removed and the mail() portion activated when corrected.)

 

function mailme() {
$to = "[email protected]" ;
$subject = "subject";
$headers = 'From:' . $_POST["emp_email"] . "\r\n" .
   'Reply-To: [email protected]' . "\r\n" .
   'X-Mailer: PHP/' . phpversion();
$_POST["emp_name"] . " has submitted \n" ;
echo $headers . "<br />";
echo $to . "<br />" ;
echo $subject . "<br />" ;
$answer=show_row($_POST["num_of_requests"]);
echo $answer;

//mail($to,$subject,$answer,$headers) ;
}

 

Okay, so I guess I don't know what I'm doing. I've used this code below to as part of a form to mail. Depending on $limit, it will repeat $limit times. This part works, and I've got no problem with it.

 

<? function request_row($limit){ 
  for ($x=1; $x<=$limit; $x++) 
  echo"<tr><td>Server Name: <input type=\"text\" size=\"20\" name=\"server$x\"></td>" .
"<td>Location: <input type=\"text\" size=\"20\" name=\"location$x\"></td>" .
"<td>Switch: <input type=\"text\" size=\"20\" name=\"switch$x\"></td>" .
"<td>Port: <input type=\"text\" size=\"20\" name=\"port$x\"></td></tr>" ;
}

 

However, I don't know where to go from here. Since I'll have an unknown number of variables created each time, I have to find a way to get those into the mail() portion of the form. If it was a known number each time, then I could work it into the results. However, I don't know how to duplicate my feat from above.

 

You can see my previous attempts at making this work in my previous posts.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.