• I’d like to use a dropdown combobox with a Submit button (a form) to reload a page passing the selected item from the combobox. Then the page would use custom PHP code to output the result.

    Example: User selects a US State from the dropdown box, presses Submit, the page reloads and executes the PHP code with retrieves all the content for the selected US State.

    In regular PHP I would simply create an html form with <form method=”post” action=”mypage.php”> but I can’t get it to work in WP. Is there a special way to do old fashioned HTML forms in WP? No matter what I put into action=””, it always seeks a non-existent page.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Set the form action page to an actually WP page. Design a page template associated only with that page. The page template then can include your custom php. You can even fully control the header, footer, sidebars(s), widgets within your custom page template.

    Always use a child theme for such work and review the Codex for these topics.

    Tip: Most, if not all, of what you want done with the form can been done with a well done WP Plugin such as GravityForms.

    Thread Starter veedot2

    (@veedot2)

    What exactly do I put inside of action="???" ?

    When I look at the URL of the page I have PHP code in, the URL looks like “https://mywebsite.com/the-page/&#8221;. When I make that address the target of action, and when I press Submit, I get a WP error about “Not Found”. Should the URL be the name of the template PHP file?

    Thread Starter veedot2

    (@veedot2)

    By the way, this explains what I’m trying to achieve overall…
    https://www.c-sharpcorner.com/UploadFile/051e29/dropdown-list-in-php/

    I want to create a dropdown menu, let the user select something, press Submit, then pass the selection back to the page so that an SQL query could be run with PHP code. I just can’t get WP to let me submit a form to pass the selected dropdown entry.

    Thread Starter veedot2

    (@veedot2)

    Ok, so I figured it out…
    The form code needs to look something like this…
    <form name="contactform" id="form" method="post" action="<?php echo get_permalink($page_id); ?>">

    And, the form components can’t use common names like “name”.
    e.g. I had to do:
    Name: <input type="text" name="xname">

    Now I can read the submitted contents of xname from within the same PHP page.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘PHP Form Inside WordPress’ is closed to new replies.