• Hi. I added a simple one field form on a page. The user enters a value in that field and submits. Then that value is assigned as a variable in my function before returning. For example, my user enters a zip code in the field. Based on that zip code, my function returns all users in the zip code. I just need to know how to get that value assigned as a variable in my function.

    Thanks for your help.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    Depending if the form action is GET or POST, all form values are assigned to the super global array $_GET or $_POST. (Also to $_REQUEST, but using that array involves a small security risk)

    Assuming your form is using POST and the field’s name is “zip”, you assign the submitted value to a variable on the page receiving the form action like so:

    if(isset($_POST['zip']))
       $zip = $_POST['zip'];

    Always check if a value is set because if the field was left blank, an error is thrown trying to assign a non-existent value to $zip.

    Thread Starter DRandy

    (@drandy)

    I am using a shortcode on the page and in that function it returns all users in that zip code. So when I wrote the function I just hard coded in the zip as a variable to test out the function. Now that it’s working, I am now trying to add the form field so a user can enter the zip code value in the form.

    I’m not very good a PHP so thanks for your patience.

    I’m now trying to put the form in the function so the shortcode will display and do everything. This is the form I’m trying to add.

    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    <label>Enter Your Zip Code:</label>
    <input type="text" name="zip" value="<?php return $zip;?>">
    <input type="submit" name="submit" value="Submit">
    </form>

    Is this all wrong? I’ve tried adding the form directly from the edit page but that doesn’t display correctly.

    Yes, it is wrong. But it’s not to bad for someone that admits they aren’t great with PHP so don’t be concerned.

    The main problem that you have is the return in there. From what I can see, that doesn’t work the way that you think it does. return returns a value form a function and when you call that it stops any processing below it, so it will stop a lot in your script.

    What you’re looking for is something like this…

    <?php
        $zip = "";
        if( isset( $_POST ["zip"] )) $zip = $_POST ["zip"];
    ?>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    <label>Enter Your Zip Code:</label>
    <input type="text" name="zip" value="<?php echo $zip; ?>">
    <input type="submit" name="submit" value="Submit">
    </form>
    Thread Starter DRandy

    (@drandy)

    OK. I went to the admin side “Edit Page” and added just the form using the “Text” tab. I took out the action so it’s just action=””. It works just like I want it to. But I think that means it’s open to attack.

    Then I tried adding your form like you have above but that gives me a ““>” characters just before the “Enter Zip Code” label.

    Then I tried a php to shortcode plugin and put the echo htmlspecialchars($_SERVER[“PHP_SELF”]); in a shortcode called “zipform” and tried putting action=”[zipform]” in the form. When I submit, it takes me to my home page.

    What am I doing wrong?

    Thread Starter DRandy

    (@drandy)

    I’ve also tried to add the form to the function rather than through the admin “Edit Page” and from what I’ve tried, it doesn’t work. maybe I’m writing it wrong. I’m stumped.

    Moderator bcworkz

    (@bcworkz)

    Honestly, I think your approach is flawed. WP text tab is not the best way to test HTML unless that HTML actually must be part of that post. IMO, HTML in the post editor should be limited to simple styling. More complex HTML should be implemented as a custom page template.

    The various PHP in post plugins can be very useful, but again, they are not the best way to test PHP. I personally use a custom test plugin to test all of my WP PHP.

    I’m not sure what your ultimate goal with this is, so this next advice could be off base, but consider creating a custom page template to display your form. Depending on what happens after the form is submitted, this template could be used to handle form submits as well. The code separating displaying the form and handling the submit is typically done by checking $_SERVER['REQUEST_METHOD'].

    If it’s “GET”, display the form. If it’s “POST”, handle the form submit. To use the template, create a new page, assign a title, specify your template, then publish. No content is required if the template is handling all of it.

    I believe using $_SERVER["PHP_SELF"] in this context may not work. It’s correct for straight PHP pages, but not for template files. I’m not aware of exploits when action="" is used, but I’m no security expert. Not specifying an action results in the form submit being sent to the same URL as used for the form, which is perfect when the page checks the ‘REQUEST_METHOD’. If it helps with security, you could use the page’s permalink as the action attribute.

    If the form must be displayed via shortcode, that is fine, but just display the form with the action still pointing to the page with your custom template. In this case, the GET HTML of the template could still be the form or perhaps instructions on what to do to see the form.

    Thread Starter DRandy

    (@drandy)

    Ok. Maybe I should use GET because all I am doing is displaying some values from the database after the user enters a zip code. Nothing more. Like I said above, everything works like it should with the action=””. I’m just not sure if that leaves it open for attacks. I’ll do more research and thanks for your help.

    How do you get a table field value into a function variable?

    I am sure this is beginner stuff to most but I am a beginner.

    I am operating a job board and am simply trying to get a field value out of a custom field and am having trouble with the syntax.

    I am not sure how to point to the table where the data is (path). The date resides in a table called “Jobs” and the code is as follows:

    <<td class="wpjb-column-date wpjb-last">
    <        <?php if($job->isnew()): ?><span class="wpjb-bulb"><?php <_e("new", "wpjobboard") ?></span><?php endif; ?>
    <        <?php echo wpjb_date_display("M, d", $job->job_created_at, <true); ?>
    <        <?php if(isset($job->getTag()->type[0])): ?>
    <        <span class="wpjb-sub" style="color:#<?php echo <$job->getTag()->type[0]->meta->color ?>">
    <        <?php esc_html_e($job->getTag()->type[0]->title) ?>
    <        </span>
    <        <?php endif; ?>
    <    </td>

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    I would like to add an argument to the “IF” statement that says if the “Priority” field has a value of “Yes” then execute “class=”wpjb-bulb-pri” which be the first of the “IF” statements and this “IF” statement would terminate.

    Any help would be appreciated.
    https://www.driverjoblist.com

    Driver64, your question is not relatesd to this issue, and you’re posting on a thread that’s 7 months old. As per the forum welcome, please start your own thread for your own question.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How do you get a form field value into a function variable’ is closed to new replies.