0

I'm trying to pre-populate my form values with variables passed through the URL.I've tried many different solutions, sometimes I don't get an error, the variable just doesn't show up. Any help is appreciated, thanks!

URL Example: website.com/?firstname=john

Code:

    <html>

<script type="text/javascript">
    function writeform(){
        selobj = document.getElementById('selform');
        newobj = document.getElementById('newform');
        p = document.getElementById('menu').selectedIndex + 1;
        a = document.getElementById('menu2').selectedIndex + 1;
        if((p < 14 && (a == 1 || a == 2 || a == 3 ||a == 4)) { // write the 1st form
        txt = 'Person 1: '+'<input type="text"/><br/>';
        txt += 'Person 2: '+'<input type="text"/>';
        } else {
            document.getElementById('div1').style.display='block';
        }
        // if(p==2 && a==1){ // write the 2nd form
        //  txt ='Name: '+'<input type="text"/><br/>';
        //  txt+='Addr: '+'<input type="text"/>';}
        newobj.innerHTML=txt;selobj.style.display='block';
    }
</script>

<div style="width: 400px; float:left;"> <?php echo $_GET["firstname"]; ?></div>
<div style="width: 400px; float: left;"> <!-- Primary Div-->

    <p style="font-size: 16px; font-weight: bold;">Select Something</p>
    <div class="fancy3">
        <table style="width:350px; height=350px">
        <tr>
            <td>
                <select id="menu" size="14">
                    <option selected="selected"><b>--- Common Options ---</b></option>
                    <option></option> //NY
                </select>
                <br/>
                <p style="font-size: 16px; font-weight: bold;">Range</p>
                <div class="fancy3">
                    <table style="width:350px; height=350px">
                        <tr>
                            <td>
                                <select id="menu2" size="4">
                                    <option selected="selected">1</option>
                                    <option>2</option>
                                    <option>3</option>
                                    <option>4</option>
                                </select>
                                <br/>
                            </td>
                            <td>
                                <div id="selform" style="display:none">
                                    <fieldset>
                                        <div id="newform"></div>
                                    </fieldset>
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
                <br/>
                <button onclick="writeform();">Search</button></td>
                <td>
                    <div id="selform" style="display:none">
                        <fieldset>
                            <div id="newform"></div>
                        </fieldset>
                    </div>
                </td>
            </tr>
        </table>
    </div>

</div> <!-- Primary Div closing tag-->


<!-- of Field-Specific Forms-->
<div id="div1" style="display:none;">

    <form action="http://site1.com/upload" method="get"> 
        First Name: <input name="fname" type="text" value="" />
        Last Name: <input name="lname" type="text" />
        Address: <input name="address" type="text" /> 
        Zip Code: <input name="zip" type="text" /> 
        State: <input name="state" type="text" /> 
        <input type="submit" />
    </form>
</div>

<div id="div1" style="display:none;">

    <?php
        $firstname = $_GET["firstname"];
    ?>  

    <form action="http://site1.com/upload" method="get"> 
        First Name: <input name="fname" type="text" value="<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>" />
        Last Name: <input name="lname" type="text" />
        Address: <input name="address" type="text" /> 
        Zip Code: <input name="zip" type="text" /> 
        State: <input name="state" type="text" /> 
        <input type="submit" />
    </form>
</div>

<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>

</html>
2
  • 1
    You might want to clean up your code. You have CSS issues height=350px and random elements that have no values. Commented May 23, 2013 at 4:44
  • An additional advice for this mess you pasted here.. Once you defined a variable, $firstname = $_GET['firstname'] for example, you can use it until the script stops from executing. Defining $firstname with the same value 3 times makes no sense. Commented May 23, 2013 at 4:48

4 Answers 4

1

Test that what you get in $_GET variable by using var_dump($_GET), then use:

echo isset($_GET["firstname"]) ? $_GET["firstname"] : "";
Sign up to request clarification or add additional context in comments.

Comments

1

Firstly use print_r($_GET) at the begining of the file to check wether you have the parameters passed.

Then you might want to clean up that mess, because defining $firstname 3 times with the same value just to echo it out makes no sense.

Secondly, you would really like to change those action url as I'm pretty sure it's wrong:

 <form action="http://site1.com/upload" method="get">

Thirdly, your input names are name="fname" meanwhile using firstname in $_GET. Not really sure if you will ever relate these two but, whatever.

Some advices:

  • learn to write code quite more readable than this.

  • go to jQuery.com and do some research, as it really helps you write less , do more.

  • CSS doesn't use equal (=) sign as value setter , which in your case is height=350px when it should be height: 350px;.
  • Give elements some ID's or Classes and use some .css files , it will clean your code more than you can imagine.

Comments

0

You had started wrongly, that's why URL doesn't appering

<script type="text/javascript">



 function writeform(){
    selobj=document.getElementById('selform');
    newobj=document.getElementById('newform');
    p=document.getElementById('menu').selectedIndex+1;
    a=document.getElementById('menu2').selectedIndex+1;
    if((p<14 && (a==1 || a==2 || a==3 ||a==4)){ // write the 1st form
      txt ='Person 1: '+'<input type="text"/><br/>';
      txt+='Person 2: '+'<input type="text"/>';} else {
      document.getElementById('div1').style.display='block';
      }
    // if(p==2 && a==1){ // write the 2nd form
    //  txt ='Name: '+'<input type="text"/><br/>';
    //  txt+='Addr: '+'<input type="text"/>';}
    newobj.innerHTML=txt;selobj.style.display='block';}

    </script>

    <body>
        <form action="http://site1.com/upload" method="get">
        <?php echo $_GET["firstname"]; ?>

    <p style="font-size: 16px; font-weight: bold;">Select Something</p>
    <div class="fancy3"><table style="width:350px; height=350px">
      <tr><td><select id="menu" size="14">
      <option selected="selected"><b>--- Common Options ---</b></option>
      <option></option> //NY
      </select><br/>

      <p style="font-size: 16px; font-weight: bold;">Range</p>
      <div class="fancy3"><table style="width:350px; height=350px">
      <tr><td><select id="menu2" size="4">
      <option selected="selected">1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option></select><br/>
      </td>
      <td><div id="selform" style="display:none">
      <fieldset><div id="newform"></div></fieldset></div>
      </td></tr></table></div>
      <br/>
        <button onclick="writeform();">Search</button></td>
      <td><div id="selform" style="display:none">
      <fieldset><div id="newform"></div></fieldset></div>
      </td></tr></table></div>

      </div> <!-- Primary Div closing tag-->


      <!-- of Field-Specific Forms-->
      <div id="div1" style="display:none;">

      <form action="http://site1.com/upload" method="get"> 
    First Name: <input name="fname" type="text" value="" />
    Last Name: <input name="lname" type="text" />
    Address: <input name="address" type="text" /> 
    Zip Code: <input name="zip" type="text" /> 
    State: <input name="state" type="text" /> 
    <input type="submit" />
    </form>
      </div>

    <div id="div1" style="display:none;">

    <?php
    $firstname = $_GET["firstname"];
    ?>  


    First Name: <input name="fname" type="text" value="<?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>" />
    Last Name: <input name="lname" type="text" />
    Address: <input name="address" type="text" /> 
    Zip Code: <input name="zip" type="text" /> 
    State: <input name="state" type="text" /> 
    <input type="submit" />
    </form>
      </div>

    <?php $firstname = $_GET["firstname"]; echo "$firstname"; ?>
</body>    
</html>

1 Comment

This user had not started <FORM> after <body> <tag>, he started <Form> in wrong manner, that's why no URL appears
0

A couple of problems that I'm seeing here ... for one you have multiple elements with the same id (see id="selform").

To load get variable into a text input the pattern is like this:

<input type='text' name='fieldname' value='<?= isset($_GET['field'])?$_GET['field']:"") ?>'/>

for a checkbox or radio control it is like this

<input type='checkbox' name='fieldname' value='myval' <?= isset($_GET['field']) && $_GET['field'] == 'myval'?"checked=\"checked\"":"") />

for select boxes it you do this:

<select name='fieldname'>
     <option value='myval' <?= isset($_GET['field']) && $_GET['field'] == 'myval'?"selected=\"selected\":"" ?>>My Val Label</option>
     <option value='myval2' <?= isset($_GET['field']) && $_GET['field'] == 'myval2'?"selected=\"selected\":"" ?>>My Val2 Label</option>
</select>

Here is a nifty select box function that will allow you too more concisely output a select in your code (i find the check with every element a little tedious)

function showSelect($name, $options, $selected, $attr = array()){
    $str = "<select name='".$name.'"';
    foreach($attr as $name=>$val){
        $str.= " ".$name."='".$val."'";
    }
    $str.=">";
    foreach($options as $k=>$val){
        $str.= "<option value='".$val."'".($val==$selected?" selected='selected'":"").">".$k.'</option>';
    }
    $str.="</select>";
}

and you can use it like this...

$days = array();
for($d = 1; $x<=31; $x++){
     $days[(string)$d] = (string)$d;
}

echo showSelect("formDays", $days, $_POST["formDays"], array("id"=>"formDays"))

2 Comments

Thanks for the help! I know my code is a mess, I'm new at all this any help is appreciated. What exactly does this do that 'my way' doesn't? Or is it just better practice? Other than actually work! Lol
for the checkbox and select implementations is just sets the selected or checked attribute on the relevant option based on what is supplied in the post instead of it being set when you. Right the template. for example in your code the menu2 element always has selected applied to the first option. This will happen regardless of what the user posted. The function that I have in there is just shorthand to create the select element for you, I find it a little more readable then the isset comparison method.

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.