2

Learning my first php search form, be gentle!

I'm looking to fill the 'value' with the number input on the search form. Everything I have tried either breaks the page or has no effect.

I'm trying to input my $input_price into the value part of my argument.

Here is my search form

<form method="post" action="http://mysite/searchresults">
<fieldset>

<!-- TEXT INPUT SELECTION -->
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
<!-- END TEXT INPUT SELECTION -->
<input type="text" name="input_price"/>
<input type="submit" id="searchsubmit" value="Search" />
</fieldset>
</form>

And here is the receiving page

<?php 


    $args['meta_query'][] = array(
            'key' => 'price',
            'value' => '($input_price)',
            'type' => 'numeric',
            'compare' => '<=',
        'order' => 'DSC'
    );
    $the_query = new WP_Query( $args );

?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) :                 $the_query->the_post(); ?>

    <h1><?php the_title() ;?></h1>          
    <?php the_excerpt(); ?>

<?php endwhile; else: ?>

    <p>Sorry, there are no posts to display</p>

<?php endif; ?>

Thanks!

1 Answer 1

1

Firstly, your data is being sent to the receiving script via the POST HTTP method, so you can access those variables with PHP's $_POST superglobal using the element's name attribute as the key:

$input_price = $_POST['input_price'];

Next, the brackets around $input_price should not be used here. Finally, single quotes do not parse variables inside them, as in this example:

'value' => '($input_price)',

But double quotes will, or simply not using quotes at all:

'value' => "$input_price",
'value' => $input_price,
Sign up to request clarification or add additional context in comments.

1 Comment

You make it sound so easy. Thank you for the answer as well as an explanation so i don't make the same mistake! This makes my day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.