I am totally stuck here and found tens of samples on posting to get and set values. What I am trying to do is -

1) Let a user enter a vehicles year model into a textbox in my form (set to post)
2) I then need to get this value to a variable state

$vehicle_year = $_GET['vyear'];

First error is here... vyear is the name and id for my textbox. Error - Undefined index 'vyear'. There is no submission of the form etc, because I am still on the same form/page.

3) With this value captured, I then search my database to return all of the manufacturers that has a year (as returned) attached to it -

$query = "SELECT * FROM `vehicledata` WHERE `year`='$vehicle_year'";

Obviously it does not work because I still do not have the value as yet returned from above with the undefined error. I've tried to change the name and id of the textbox, no luck.

4)Once these records has been returned, I need to add the values to a select (drop down) box. I have no idea how to get the values in there - seems I need to run a loop, which I know how to, just don't know how to add the options to the select box.

Any help will be appreciated, thanx guys.

P.S. I will add against sql injection once I know how to get the values and add the options. Just need the basics to get me going... :)

Recommended Answers

All 9 Replies

If you are using post, then do:

$vehicle_year = isset($_POST['vyear']) ? $_POST['vyear'] : -1;

If the year is -1 then it was not set/posted yet.

Thanx Pritaeas (and hello)

Just tried the code, still get the same error -

Undefined index: vehicleyear in C:\xampp\htdocs\JustMyWheels\search.php on line 38

Please note that the form is not yet posted when I need the value. I need to get the value immediatly after the user entered it - here's my code if that will help..

<table class="blue">
        <h2 class="innertable">Search Just my Wheels</h2>    
          <form action="" name="frmsearch" method="POST" class="innertable">
                <td align="right"><strong>Year</strong>&nbsp;&nbsp;</td>
                <td><input type="text" size="6" name="vehicleyear" id="vehicleyear" class="searchbox" value="2010" onchange="clearlist();" onblur="if(this.value.length&gt;=4){if (this.value!='') {if (!RegExp('^((19[5-9])|(20[0-9]))[0-9]$').test(this.value) || this.value&gt; new Date().getFullYear()) {alert('Invalid year, please re-enter carefully');window.setTimeout('doc(\'vehicleyear\').focus()',1);this.value=''};getTypes();}}else{cleardropdown();}" onkeyup="if(this.value.length&gt;=4){if (this.value!='') {if (!RegExp('(19[5-9])|(20[0-9])[0-9]').test(this.value) || this.value&gt; new Date().getFullYear()) {alert('Invalid year, please re-enter carefully');window.setTimeout('doc(\'vehicleyear\').focus()',1);this.value=''};toggleVisibility('make');toggleVisibility('makelabel');}}else{cleardropdown();}" />
              <td width="20px">


                //This is where the error occurs AFTER the check was done above that a value has been entered...
                $vehicle_year = isset($_POST['vyear']) ? $_POST['vyear'] : -1;

                    $vehicle_year = $_GET['vehicleyear'];
//$query = "SELECT * FROM (SELECT * FROM `vehicledata` WHERE `year`='$vehicle_year' ORDER BY `cid` DESC LIMIT 1, 10) AS `table` ORDER BY `cid` ASC";
$query = "SELECT * FROM `vehicledata` WHERE `year`='$vehicle_year'";

$result = mysql_query($query);

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $vehicle_id = $line['cid'];
    $vehicle_year = $line['year'];
    $manufacturer = $line['carfindmake'];

    //Get manufacturer data...
    $images = get_make($vehicle_year);

    if (!empty($make)) {
        echo 'No data';
    } else {
        echo 'Found stuff';

                <td align="right"><span style="visibility:hidden" id="makelabel"><strong>Make</strong>&nbsp;&nbsp;</span></td>
                <!--<td><input name="make" id="make" type="text" class="searchbox" style="visibility:hidden" onClick="toggleVisibility('modellabel'); toggleVisibility('model');"/></td>-->
                <td><select name="make" id="make" class="searchbox" style="visibility:hidden" onClick="toggleVisibility('modellabel'); toggleVisibility('model');"/></td>

Line 19 undoes what line 16 does. Remove it, and change $_POST['vyear'] to $_POST['vehicleyear']

Hahaha, man, hit me with a pole. I've tried so many ways that I missed that part in full, thanx.

It does however give me a -1 when I try to echo out the value. I presume that is because it could not get a value from vyear right? That puts me back to square 1 again...

I have removed the test value from vehicleyear "2010", it needs to be entered by the user and THEN the value needs to be returned so I can run the select statement. Where am I stuck, any idea?

I defaulted the value to -1 so that you could use an if statement. If the year equals -1 do not execute the query, because you know you will not get a result.

Thanks again, I've used the if statement, no problem. Problem is that I am still left with an entered year model that i need to return. Any ideas on how I CAN get the value, I need it as you've seen above to run my sql select statement with the year as criteria?

This is what I did for testing purposes...

$vehicle_year = isset($_POST['vehicleyear']) ? $_POST['vehicleyear'] : -1;

                if ($vehicle_year == -1) {
                    echo 'No Value Returned...';
                } else {
                    //How to return this value..
                    echo $vehicle_year;

Am not quite sure, you have the value. What do you want to do with it? You can just use the code you had, unless I completely misunderstand. Just put your query in the else block.

Member Avatar for Zagga

Hi AndreRet,

Sorry if I have missed something, but $_POST['vehicleyear'] won't be available until the form has been submitted. I don't see a submit button.

Correct Zagga, which was my question - how to get a value without submitting/posting a form. :)

I have in the mean time changed to select boxes, posting the form everytime a user clicks on a selection. Thanx Zagga and Pritaeas.

I have a new question, which will be under a new thread, coming from this. All help there will be appreciated, thanx.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.