• I’m brand new with PHP and now had to jump into the ocean with no swimming lessons. So I appreciate the help! I’m having a problem with a PHP page on our WordPress site.

    I am trying to populate a second dropdown with database values based on the first one. I have a couple problems, but let me deal with the first one:

    All the tutorials say the best way to do this is with a form reload on a Javascript onchange event. But…. WordPress is changing names for the site by stripping extensions and adding a trailing “/”. For example, my page is named state_selection.php. WordPress turns the address to /state-selection/.

    When I reload the page on the form reload, my value is like this: /state-selection/?state=VA. It comes back as Error 404 since that’s not the page.

    I know I have other issues since the Submit button doesn’t show on the first load of the page. Here is my code:

    The JS script is in the header.php file:
    <SCRIPT language=JavaScript>
    function reload(form){
    var val=form.state.options[form.state.options.selectedIndex].value;
    self.location=’state_selection.php?state=’ + val ;
    }
    </script>

    I have tried
    self.location=’state-selection/?state=’ + val ;

    But I still get the Error 404.

    Is there any way to pass the state value without a reload form?

    BTW, I have done the state select as an array, and when that didn’t work, I did a straight HTML box. I abbreviated it here for sake of space.

    Here is the main page code:
    <?php /* Template Name: State and Employer Selection*/ ?>

    <?php get_header(); ?>

    <div id=”main_content”>
    <div class=”stateemp”>
    <p>PLEASE SELECT YOUR STATE IN THE DROP DOWN BOX BELOW:</p>

    <form action=”state_selection.php” method=”post” name=”state”>
    <select name=”state” onchange=”reload(this.form)”>
    <option value=”” selected=”selected”>Select state</option>
    <option value=”AL”>Alabama</option>
    <option value=”AK”>Alaska</option>
    <option value=”AZ”>Arizona</option>
    <option value=”AR”>Arkansas</option>
    <option value=”CA”>California</option>
    <option value=”CO”>Colorado</option>
    </select>

    <p>PLEASE SELECT YOUR EMPLOYER’S NAME:</p>

    <select name=’employer’>
    <option value=”>Choose Employer</option>

    <?php

    // set database server access variables:
    $host = “localhost”;
    $user = “youradmi”;
    $pass = “Admin8Partners”;
    $db = “youradmi_demographics”;

    // open connection
    $connection = mysql_connect($host, $user, $pass) or die (“Unable to connect!”);

    // select database
    mysql_select_db($db) or die (“Unable to select database!”);

    $state=$_GET[‘state’];

    // create query
    if(isset($state) and strlen($state) > 0){
    $quer=mysql_query(“SELECT distinct name FROM employer_state state=$state order by name”);
    }else
    {$quer=mysql_query(“SELECT distinct name order by name”); }

    // execute query
    $result = mysql_query($query) or die (“<p>Error in query: $query. “.mysql_error());

    // printing the list box select command

    while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
    echo “<option value=$nt[id]>$nt[name]</option>”;
    }

    // close connection
    mysql_close($connection);

    ?>

    </select>

    <input type=”submit” name=”mysubmit” value=”Submit Form” />
    </form>
    </div>
    </div>

    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Thank you for your help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter adminmark

    (@adminmark)

    Thank you, but it says it does not work with our version of WordPress.

    Have you tried the gravity forms plugin? That is what I would use. If nothing else, maybe look at some other plugins that are free that might have some code that would help you – TDO Mini Forms is kind of buggy in some server environments (not sure if it is 3.2.1 compatible either) but it might help you find an answer:

    https://www.ads-software.com/extend/plugins/tdo-mini-forms/

    Thread Starter adminmark

    (@adminmark)

    Thanks for replying!

    Gravity forms look great! Unfortunately, it won’t let me fill a dropdown from a database based on a selection in another dropdown. ??

    I don’t know the full details of your project, but if all you want to do is display one form with a list based on what they chose from the first drop down menu, you can definitely do that with Gravity Forms, I just tested it ??

    To do it you just set up one drop down listing all of your states, then you set up a drop down for each state with a list of the employers, then under advanced settings choose which state it is for from the first drop down menu you made.

    Anyways, that is just what I would do. Especially nice also because you could easily add to/change later.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘PHP, Javascript and WordPress problem’ is closed to new replies.