Forum Moderators: coopster

Message Too Old, No Replies

Converting ASP to PHP

         

dave1236

8:33 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



All:

Any and all help is requested. I am currently migrating from an asp environment to php and need assistance converting my scripts. I am trying to pass a value from one page to another.

with asp, i use the following:

First page:
<form name="form1" method="post" action="XXX.asp">
<select name="ABC" id="ABC">
<option value="na">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
</select>
<input type="submit" name="Submit" value="Go">
</form>

Second page:
named XXX.asp

code:
<a href="http://www.example.net/click-123-456?SID=<%=request.form("ABC")%>" target="_blank">DEF </a>

What PHP code will place the value of the selection (A,B,C, or D) in the <%=request.form("ABC")%>" spot?

Thanks in advance!

[edited by: coopster at 4:57 pm (utc) on Sep. 20, 2004]
[edit reason] generalized url [/edit]

coopster

8:58 pm on Sep 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, dave1236!

<a href="http://www.example.net/click-123-456?SID=<?php print $_POST['ABC']; ?>" target="_blank">DEF </a>

PHP has superglobals [php.net] that you'll want to read up on. Some other areas are an introduction [us4.php.net] and a simple tutorial [us4.php.net]. Best regards!

P.S. Our PHP Forum Library [webmasterworld.com] is also packed with information.

dave1236

12:06 am on Sep 19, 2004 (gmt 0)

10+ Year Member



Coopster:

Exactly what I was looking for! Thank you very much. If I may ask one follow-up question...if I had two variables I wanted to pass to between pages, how would I change my code?

Thanks.

dave1236

12:33 am on Sep 19, 2004 (gmt 0)

10+ Year Member



One additional piece of info regarding my inquiry.

If "A" is selected, I would like to pass the number 1 to one place, and the number 2 to another.
If "B" is selected, pass 3 and 4. and so on.
So in essence, I am looking to populate my php page with 3 variables.

mincklerstraat

9:14 am on Sep 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On your first question:
In general, PHP pages have variables passed to them from forms in specific arrays:
If you use the method GET in your forms (or if they're simply urls with parameters), the page will receive these variables as the array $_GET['name'] (include the quotes, since the value there is a string) - where name is the name you assigned to a form field, or the name of the parameter in the url.
If you use POST in your forms, your php page has these accessible to it in the form of $_POST['name'].
One of the most basic ways of passing variables between pages is to use hidden input fields - check w3schools.com on HTML forms for more info on those - or you can use the more complicated, but often easier, 'sessions' method - [be2.php.net...] - though I wouldn't recommend this to very new programming newbies.

Your second question would be answered with the answer above and some kind of 'if some variable value is A, then print out 1' statement in php - like,
if($_POST['ABC'] == 'A') {
echo '1';
} elseif($_POST['ABC'] == 'B') {
echo '3';
}
at that one point; this same statement except with echo '2' and echo '4' replacing the above lines beginning with 'echo'. (echo is more or less like print).

for more on this 'if' business, see [be2.php.net...]

dave1236

2:54 pm on Sep 19, 2004 (gmt 0)

10+ Year Member



Minck:

What you suggest seems to me (a newbie to PHP) very complicated. I guess what I was wondering was how I could incorporate a second value in my form and then post it in a spot on the XXX.asp page, just like the answer from coopster. The reason I want to do it this way, if possible, is that each selection (a,b,c,d, etc.) has a unique number assigned to it, and it seems that my coding would be extensive if i used a large number of if statements to gather the proper number. Maybe I just don't understand the implementation of your response. Thanks.

I guess this is what I was hoping for....

<form name="form1" method="post" action="XXX.asp">
<select name="ABC" id="ABC">
<option value="na">a</option>
<option value="b" "number=1" >b</option>
<option value="c">c</option>
<option value="d">d</option>
</select>
<input type="submit" name="Submit" value="Go">
</form>

See option "b" for what i was searching for. Thanks again.

mincklerstraat

6:34 pm on Sep 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't think you can set two variables like that in one select box - you'd need another select box to select 1, 2, ... problem is also, once you've put these numbers into your forms, what are you going to do with them? Sooner or later you'll come to 'basic' PHP which is a step beyond just <?php print $var;?> and need conditional statements (if's). Don't be afraid to get your feet wet - visit apachefriends.org to get php installed on your own computer to try stuff out, and read those tutorials coopster posted. Good luck whatever you do!

dave1236

10:32 pm on Sep 19, 2004 (gmt 0)

10+ Year Member



Minck:

Once I get the two variables assigned, I am simply appending them to a link, like is done with the first POST from Coopster. Also, one variable is going to be assigned a distinct URL based on the selection of a,b or c.

At this time, I do not need to do anything with them except populate my linking fields and create my new links.

dave1236

2:04 am on Sep 20, 2004 (gmt 0)

10+ Year Member



Minck:

I tried your "if" statements. they work as expected. There is no limit to the number of "ifs" i use, correct...because I will likely be adding over 200 of these statements to the code. if it works...i do not kow what i am complaining about, except that it seems to be a very long way to code to get the desired result.

Thanks again!

mincklerstraat

8:40 am on Sep 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, no theoretical limit to the number of if's you can use - if you're looking at 200, be sure to check out switch() too. When you learn more about some other functions and arrays, you'll probably use only a small fraction of that number of if's by coding more efficiently. Good luck with your project.

dave1236

12:37 pm on Sep 20, 2004 (gmt 0)

10+ Year Member



Minck:

Thanks for the "switch" function. I will give it a try...looks a little easier to code and maintain.

There is soooooo much to learn.