I have the following scenario -

User clicks on an image to search for a particular brand vehicle. The value of that image must then go the hidden box where I will post the value returned to another form. I just can't seem to get the value returned...

My code...

<form action="searchbrand.php" method="POST" name="frmbrand">
 <!--First row of logos...-->   
<table class="whitelogo">
    <tr>
        <td align="center">
            <p style="height:5px"></p>
            <input type="image" name="brand" id="brand" value="Alfa Romeo" src="images/carlogos/alfa.jpg" height="55px" width="55px" title="View our Alfa Romeo Selection" alt="Alfa Romeo" onClick="javascript:document.submitForm.submit();" />
        </td>
        <td align="center">
            <p style="height:5px"></p>
            <input type="image" name="brand" id="brand" value="Aston Martin" src="images/carlogos/aston.jpg" height="55px" width="55px" title="View our Aston Martin Selection" alt="Aston Martin" onClick="javascript:document.submitForm.submit();" />
        </td>
        <td align="center">
            <p style="height:5px"></p>
            <input type="image" name="brand" id="brand" value="Audi" src="images/carlogos/audi.gif" height="55px" width="55px" title="View our Audi Selection" alt="Audi" onClick="javascript:document.submitForm.submit();" />
        </td>
        <td align="center">
            <p style="height:5px"></p>
            <input type="image" name="brand" id="brand" value="Bentley" src="images/carlogos/bentley.jpg" height="55px" width="55px" title="View our Bentley Selection" alt="Bentley" onClick="javascript:document.submitForm.submit();" />
        </td>
        <td align="center">
            <p style="height:5px"></p>
            <input type="image" name="brand" id="brand" value="BMW" src="images/carlogos/bmw.gif" height="55px" width="55px" title="View our BMW Selection" alt="BMW" onClick="javascript:document.submitForm.submit();" />
        </td>
        <td align="center">
            <p style="height:5px"></p>
            <input type="image" name="brand" id="brand" value="Cadillac" src="images/carlogos/cadillac.jpg" height="55px" width="55px" title="View our Cadillac Selection" alt="Cadillac" onClick="javascript:document.submitForm.submit();" />
        </td>
    </tr>
</table>

As you will notice here I have named the images ALL 'brand' with seperate values for each.

<input type="hidden" name="hidbrand" value="<?=$_POST['brand'];?>"/>
</form>

I'm trying to capture the value here from whichever image user clicked on...

In my searchbrand.php page, which will then do stuff according to the returned value, the following...

<?php
include 'init.php';

$vehicle_brand = $_POST['hidbrand'];

echo $vehicle_brand, '</br>';

?>

Nothing gets returned, no error, no value, all blank like me :)
Anyone have an idea where I'm going haywire?

Recommended Answers

All 8 Replies

You are trying to capture the value of the input, e.g. Alfa Romeo ?

If you switch the submit javascript function with your own, then you can copy the clicked value into your hidden input and then submit the form. Is that what you want?

Correct, I want to copy the value of the image clicked (as you can see, there is 6 - all different values, Alfa Romeo, Aston Martin, BMW etc). Based on that value I will search my database with all manufacturer names of Alfa Romeo and then display the adverts.

I just can't seem to get the value over to my second page with my code above. If I have a look at the source code, I can see the value in the hidden box as was selected...

If the value is there, then it is strange you cannot output it. Do you have this online somewhere to see?

Unfortunately, still on localhost in development.

I have even echoed out the value in my first page, no problem. As soon as I get to the second page, no frikken value :) I've tripple checked the code, must work. no value gets echoed.

Does it makes sense that it seems the form gets submitted first and THEN the value gets added to the hidden input? If so, how can I add the value first then submit the form.

Remember that by clicking on an image is submitting it, any ideas...

I don't see how the hidden input can possibly be populated with the intended value because it is PHP that echoes the value and PHP won't do anything once the page is loaded. $_POST['brand'] needs to exist when the page is loaded for PHP to echo the value onto the page. Pritaeas idea to make your own submit function and use Javascript to populate the hidden element and then submit the form is what I would do. If I have misunderstood your intentions, please excuse me :)

Thanx Adam, but yes, you did misunderstood. The value is retrieved once the form is posted and then used in another form. The problem is that it seems that the form submits and only then does it add the value. I'm playing around with it now as we speak. :)

I got the solution. To all that replied, thank you.

Solution - Image input is a submit button on its own. When clicked on, it will submit the form. This is where I encountered the problem, the form got submitted before the value was returned. I have used the code below to return the value to the hidden input and then the form got submitted (automatically)

<!--I've changed the onclick event to-->
onClick="javascript:document.forms['frmbrand'].hidbrand.value = this.value;"

<!--I've removed $_POST from hidden input-->
<input type="hidden" name="hidbrand" value=""/>
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.