Tech Support Forum banner

[SOLVED] Variable to URL

1172 Views 19 Replies 2 Participants Last post by  Laxer
Hey all,

Trying to get variables from my HTML form into my URL using PHP. In which case the action page will use the variables from the URL to perform a query into mysql database. Any idea why the TERMVAL variable isn't getting posted into my URL?

I'm banging my head on this one and I just can't seem to get it.

Thanks!

Search Page

Code:
<?php
if(isset($_POST['SUB1']))
$TERMVAL = $_POST["TERMVAL"];

if(isset($_POST['SUB2']))
$TERMVAL = $_POST["TERMVAL"];

if(isset($_POST['SUB3']))
$TERMVAL = $_POST["TERMVAL"];

if(isset($_POST['SUB4']))
$TERMVAL = $_POST["TERMVAL"];

if(isset($_POST['SUB5']))
$TERMVAL = $_POST["TERMVAL"];

if(isset($_POST['SUB6']))
$TERMVAL = $_POST["TERMVAL"];

if(isset($_POST['SUB7']))
$TERMVAL = $_POST["TERMVAL"];

if(isset($_POST['SUB8']))
$TERMVAL = $_POST["TERMVAL"];

$url1 = "view.php?TERMVAL=";
$url2 = $TERMVAL;

$urlfin = $url1 . $url2;
?>

<form method="post" action="<?php echo $urlfin . "&TERM=DATE"?>">
    All records by date:
    <input type="text" size="8" maxlength="8" name="TERMVAL">
    <input type="Submit" value="Submit" name="SUB1">
     <br />
     <center><IMG SRC="blackbar600x12.jpg"></center>
     <br />
     </form>
    
    <form method="post" action="<?php echo $urlfin . "&TERM=OFFICER"?>">
    All records by badge number:
    <select name="TERMVAL">
    <option value="--">--</option>
    <option value="101">101</option>
    <option value="102">102</option>
    <option value="103">103</option>
    <option value="104">104</option>
    <option value="105">105</option>
    <option value="108">108</option>
    <option value="109">109</option>
    <option value="135">135</option>
    <option value="136">136</option>
    <option value="138">138</option>
    <option value="150">150</option>
    <option value="155">155</option>
    <option value="160">160</option>
    <option value="161">161</option>
    <option value="162">162</option>
    <option value="163">163</option>
    <option value="164">164</option>
    <option value="167">167</option>
    <option value="169">169</option>
    <option value="171">171</option>
    <option value="172">172</option>
    <option value="174">174</option>
    <option value="176">176</option>
    <option value="177">177</option>
    <option value="178">178</option>
    <option value="179">179</option>
    </select>
    <input type="Submit" value="Submit" name="SUB2">
     <br />
     <center><IMG SRC="blackbar600x12.jpg"></center>
     <br />
     </form>
    
    <form method="post" action="<?php echo $urlfin . "&TERM=SQUAD"?>">
    All records by squad:
    <select name="TERMVAL">
    <option value="11">11</option>
    <option value="20">20</option>
    <option value="21">21</option>
    <option value="22">22</option>
    <option value="23">23</option>
    <option value="24">24</option>
    <option value="25">25</option>
    <option value="27">27</option>
    <option value="28">28</option>
    </select>
    <input type="Submit" value="Submit" name="SUB3">
     <br />
     <center><IMG SRC="blackbar600x12.jpg"></center>
     <br />
     </form>
     
    <form method="post" action="<?php echo $urlfin . "&TERM=SGT"?>">
    All records by SGT:
    <select name="TERMVAL">
    <option value="104">104</option>
    <option value="105">105</option>
    <option value="108">108</option>
    <option value="109">109</option>
    </select>
    <input type="Submit" value="Submit" name="SUB4">
     <br />
     <center><IMG SRC="blackbar600x12.jpg"></center>
     <br />
     </form>
     
    <form method="post" action="<?php echo $urlfin . "&TERM=LT"?>"> 
    All records by LT:
    <select name="TERMVAL">
    <option value="102">102</option>
    <option value="103">103</option>
    </select>
    <input type="Submit" value="Submit" name="SUB5">
     <br />
     <center><IMG SRC="blackbar600x12.jpg"></center>
     <br />
     </form>
    
    <form method="post" action="<?php echo $urlfin . "&TERM=SIT"?>">
    All records by SI type: (not implemented)
    <input type="text" size="5" maxlength="4" name="TERMVAL">
    <input type="Submit" value="Submit" name="SUB6">
     <br />
     <center><IMG SRC="blackbar600x12.jpg"></center>
     <br />
     </form>
    
    <form method="post" action="<?php echo $urlfin . "&TERM=CN"?>">
    All records by case number: (not implemented)
    <input type="text" size="5" maxlength="4" name="TERMVAL">
    <input type="Submit" value="Submit" name="SUB7">
     <br />
     <center><IMG SRC="blackbar600x12.jpg"></center>
     <br />
     </form>
     
    <form method="post" action="<?php echo $urlfin . "&TERM=SHIFT"?>">
    All records by shift:
    <select name="TERMVAL">
    <option value="D">D</option>
    <option value="EP">EP</option>
    <option value="A">A</option>
    <option value="LP">LP</option>
    <option value="M">M</option>
    </select>
    <input type="Submit" value="Submit" name="SUB8">
    <br />
    </form>
View Page

Code:
<?php

$TERM = $_GET["TERM"];
$TERMVAL = $_GET["TERMVAL"];

$con = mysql_connect("localhost:3307","root","SUPERSECRETPASSWORD");

mysql_select_db("COMOARS", $con);

$res1 = "mysql_query(";
$res2 = '"SELECT * FROM ';
$res3 = $TERM . '")';
$result = $res1 . $res2 . $res3;

echo "<table border='1'>
<tr>
<th>Value1</th>
<th>Value2</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Value'] . "</td>";
  echo "<td>" . $row['Value'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

    
    
<br />
<center><IMG SRC="blackbar600x12.jpg"></center>
<br />
     
DIAGNOSTICS:
    
<?php
echo "<br />";
echo $TERMVAL;
echo "<br />";
echo $TERM;
echo "<br />";
echo $con;
echo "<br />";
echo $res1;
echo "<br />";
echo $res2;
echo "<br />";
echo $res3;
echo "<br />";
echo $result;
echo "<br />";
echo $url1;
echo "<br />";
echo $url2;
echo "<br />";
echo $urlfin;
echo "<br />";
?>
You can probably tell I steal alot from w3schools :p

For some reason the TERM variable from the view page loads fine but not the TERMVAL.

Ideas? Suggestions? Constructive Criticism? Thanks a bunch!

The echo's are for my own diagnostics, when I submit a search heres what I get...

Code:
DIAGNOSTICS: 
<-----THIS IS WHERE THE TERMVAL SHOULD BE ----->
LT
Resource id #2
mysql_query(
"SELECT * FROM 
LT")
mysql_query("SELECT * FROM LT")
See less See more
Status
Not open for further replies.
1 - 20 of 20 Posts
Re: Variable to URL

posting information in the url bar is not very secure. if your handling sensitive information consider changing to sessions.

anyway here is how i handle info in the url

Just a simple link: (will probably need if/else statements to get a good link)

<a href="website.com/php.php?id=<?PHP echo $id;?>">Link</a>
I then receive it on another page using
$id=$_GET['id'];;
Hope that helped :D

Relating this to your script, try sending one variable at a time then switch to 2.

everything else seems fine.

My guess is its an error in your looping/ if/else

I will diagnose it tonight if you cant figure it out. :grin:
See less See more
Re: Variable to URL

Thanks again Laxer! There shouldn't be any sensitive information going over so that's why I am using this method, a bit easier to see and script. It's just going to be basic search query variables going through URL. I also was suspecting issues in my if/else functions but I just can't pin it down. I am still a little hazy exactly on html form input and variables going to the post array and stuff, it's kinda wierd :p haha.

(btw the input you had helped me with is working great now and all the variables are being stored in the appropriate record sets, so now I am working on query and display)

THANKS!
Re: Variable to URL

No problem!

Please mark the thread as "solved"

if you run into any other problems post a new thread and i will provide input.
Re: Variable to URL

I THINK I figured why this isn't working correctly, I will have to rework the script. The $_POST'ed variables are trying to be posted to the action of the form, so in essence, if I had my form posting to awesome.php, the variable would be defined in the post array in that script as it is called through the form action. However, I am trying to get post information on the script in the same page... How would that script ever get the definition of the posted variable when it's supposed to be used in the script the HTML form actions too....

So I guess it would be more prudent to post all variable to the script the action points to, then use if/else to check if the variable is defined with isset, and if it is, to assign TERM the according value.

I am way new to PHP and mysql so if that doesn't make sense i'm sorry, I am not up to date with the jargon so articulating what I think is the problem and solution is difficult...

but then I have to make sure only one variable out of 8 choices or so is passed and that makes it a tad more strange. Wish me luck o.o

lol

Thx :3
See less See more
Re: Variable to URL

I too see the problem.

What you suggested should work..

I have ran into problems before with isset for multiple sections, if you too run into that problem use empty();

but essentially

Code:
if(!isset($_POST['submit'])) {
SEARCH PAGE CONTENT
}
else{
SOME IF/ELSE TO MAKE SURE GOOD INPUT

VIEW PAGE
}
See less See more
Re: Variable to URL

I got the variables to post over and then used a combination of if(!isset) in conjunction with unset to check for good input and to make sure only one variable is used for querying, then I used isset as well to define the variable for the appropriate field in the table to be called. Earlier I had shyed away from putting all the record sets into one table as suggested because I had just figured out how to get a single record set into a single table at a time :p Now I can see how this is going to be a pain for querying because there is no easy way to query all tables for all recordsets by a certain field. (plus all the hits on performance I read about)

So I am in the process of reworking the table and getting recordsets to submit fine witha auto-increment.

That's what I get for not following proper advice :p haha.

Thanks :3
Re: Variable to URL

i always start off my scripts i right from scratch with a few tables usually one for login info if needed and one for data.

I can then query anything with simple code to confirm the idea is correct before i improve the structure.
Re: Variable to URL

So now that I have one table which will handle all the entries from someone when they file a report, how do I assure unique record sets (is that the correct vernacular?) so that we can query different information effectively.

Say, They enter in information by the number car they are in, what shift, their employee ID, etc. I want to be able to query by all those different things. How do I know that all the query's returned by one of those variables will be attached to the correct information? IE, how would I make it so I could query by employee ID, it would show results of the reports associated with that employee ID.
Re: Variable to URL

Code:
$query = "SELECT * FROM table WHERE id LIKE '$id%' ORDER BY last";
$result = mysql_query($query) or die('Unable to process Request');
while($row = mysql_fetch_array( $result )) {
}
Remember in php "%" is a wilid card, in this case if $id was set to "M" it would return all entries with "M" starting their id.

you could also do something like:

Code:
$id=mysql_real_escape_string(($_POST['id']));
$query = "SELECT * FROM table WHERE id='$id' ORDER BY last";
where you get $id from a form the user fills out.

for multiple you cold do:

Code:
$query = "SELECT * FROM table WHERE id='$id' AND carid='$car' AND shift='$shift' ORDER BY last";
See less See more
Re: Variable to URL

I have everything running swimmingly so far :) Thanks for all your help Laxer! I have addressed all issues with your help, some googling and a little mental elbow grease. One issue remains though, displaying the querys. I am trying to do something basic just make sure it works, then elaborate from there.

Everything inputs to one table correctly and whatnot, there is 162 variables per form that get submitted.

I am only trying to display a couple of them.

Here is my code,

VIEW INPUT -
Code:
<?php
$TV1 = $_POST["TERMVAL1"];
$TV2 = $_POST["TERMVAL2"];
$TV3 = $_POST["TERMVAL3"];
$TV4 = $_POST["TERMVAL4"];
$TV5 = $_POST["TERMVAL5"];
$TV6 = $_POST["TERMVAL6"];
$TV7 = $_POST["TERMVAL7"];
$TV8 = $_POST["TERMVAL8"];

//Unset officer on default
if ($TV2 == "--"){
unset($TV2);
}

//Unset squad on default
if ($TV3 == "--"){
unset($TV3);
}

//Unset SGT on default
if ($TV4 == "--"){
unset($TV4);
}

//Unset LT on default
if ($TV5 == "--"){
unset($TV5);
}

//Unset shift on default
if ($TV8 == "--"){
unset($TV8);
}

if (isset($TV1)){
$TERM = "DATE";
}

if (isset($TV2)){
$TERM = "OFFICER";
}

if (isset($TV3)){
$TERM = "SQUAD";
}

if (isset($TV4)){
$TERM = "SGT";
}

if (isset($TV5)){
$TERM = "LT";
}

if (isset($TV8)){
$TERM = "SHIFT";
}

$TERMVAL = $TV1 . $TV2 . $TV3 . $TV4 . $TV5 . $TV6 . $TV7 . $TV8;

$con = mysql_connect("localhost:3307","root","SUPARPASSWORD....OFDOOM");

mysql_select_db("COMOARS", $con);

$res1 = "mysql_query(";
$res2 = '"SELECT * FROM REPORTS';
$res3 = '");';

$resfin = $res1 . $res2 . $res3;
 
$result = mysql_query($resfin) or die(mysql_error());

$row = mysql_fetch_array($result) or die(mysql_error());
echo $row;


mysql_close($con);
?>
The values it gets it gets from POST from an HTML form. That all works :) Woo!

It's almost fun the way you can get HTML and PHP to work together to do stuff, I feel guilty when I have fun scripting together a site at work....

I need to get a life :p

Thanks again!
See less See more
Re: Variable to URL

Here is a picture of the error it spits,



Which is strange because when I enter the parenthesis'd part of the command into phpmyadmin sql query box without it's quotes it works fine.

Ideas?
See less See more
Re: Variable to URL

TRIPLE POST! I feel so damned dirty :(

Anyways I figured it out, for some reason I was trying to put mysql_query() in mysql_query(). No idea why I did that...

Anyways, it is creating the array just fine now, now to display it and manipulate it the way I want it! :D
Re: Variable to URL

I'm glad you figured it out for yourself!

Let me know if any other problems come up :D
Re: Variable to URL

I am kind of having difficulty wrapping my head around on how to pull a whole row. I know that the $result variable stores a mysql array of the rows and tables, and the way I have it now it displays the contents of the rows based on the column I query. Say I search for badge number, it will display all the badge number from all rows regardless of the badge number I select. I am pretty sure with information you gave me it won't be to difficult to display ONLY the badge number selected out of all of the rows, but how do I display the contents of the associated row as well? Say I query for badge 108, I want it to show all rows with the column value for badge number of 108. That make sense? I did get auto-increment ID numbers working as well so I am not sure if that's something I had to get going as well or not? Displaying query'd information is another strange territory for me...

Thanks a ton, again!
Re: Variable to URL

when you do a loop for example
HTML:
while($row = mysql_fetch_array( $result )) {
while inside that loop just call the information you want.

for example:
HTML:
<table border='1' style="margin:auto auto;">"
<tr> <th>badge</th><th>First</th> <th>Last</th> <th>date</th> </tr>
<?
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['badge'];
echo "</td><td>"; 
echo $row['first'];
echo "</td><td>"; 
echo $row['last'];
echo "</td><td>"; 
echo $row['date'];
echo "</td></tr>";
?>
</table>
See less See more
Re: Variable to URL

I'm assuming the date, last, first, badge, etc etc after $row in the echos are the column values to call from the array created in $result by mysql? If so great, that's not so bad. I see mostly everywhere always calls this information into a table, is this just the easiest way of displaying the information.

Would it be possible to say,

Code:
<html>
<body>
This is a the generated report for <? echo $TERMVAL; ?>.
Badge Number: <? while....$result... { echo $row['badge'];
Date <? while....$result... { echo $row['date'];
...etc etc etc
</body>
</html>
I am assuming the use of while more than once is unessecary and I would probably want to format it a little differently, but I could go without tables, right? I know you can format table properties and that to form it up nice like it's not even a table displaying but I guess my concern is printer friendliness... say the tables just never wanted to print right, perhaps I would be able to shy away from it?

Is there a way to donate to the site? I get better help here than I would from any class or phone line it seems, i'd like to give back a bit, as I do not always have the time to constantly surf the forums and help other people out :)

Thanks a ton :D
See less See more
Re: Variable to URL

I would agree that the second while loop is unnecessary.

Yes you can output the information however you like. I always put it in a table to keep it nice and organized.

I would suggest using a table, then if you have problems use css to define the table to fit properly on the printing page.

the correct format for your above would be:

while.... date: $row['date'] badge: $row.... etc

if you wan't to just display certain information you define it in your mysql_query.


As for donating, The link is currently down and we are unaware of when it will be back up.

For now a simple thank you and some possible word of mouth advertising is more then enough :grin:
See less See more
Marking this solved as these threads to deeply digress from the original post ;) I will try to post new ones and solve them specifically and on time for different issues as they arise.

Thanks!
One thing to learn about webdesign, It is always changing.

With that said of course things will change from start to finish and new problems will occur as new items are released/updated.

As always feel free to post any comments or concerns you have about your website. Also feel free to offer some valuable advise to our less experienced web designers.
1 - 20 of 20 Posts
Status
Not open for further replies.
Top