Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Whats Wrong with this PHP?

Options
  • 12-07-2007 5:54pm
    #1
    Registered Users Posts: 7,041 ✭✭✭


    First let me say that I'm new to PHP. This is my first script bigger than echoing Hello World. The following script may be totally wrong and confusing so be warned.

    [PHP]$label = array( 'Fname' => 'First Name:',
    'Sname' => 'Surname:',
    'Address1' => 'Address 1:',
    'Address2' => 'Address 2:',
    'Town' => 'Town/city:',
    'County' => 'County:',
    'Hphone' => 'Phone No.:',
    'Mphone' => 'Mobile No.:',
    'Appliance' => 'Appliance Type:',
    'Brand' => 'Appliance Brand:',
    'Snumber' => 'Serial No.:');

    echo '<div id="form">
    <form action="ENTER_LINK.php" method="POST">
    <p id="leftCol">
    {$label}<input type="text" name="Fname" value="$Fname" />
    {$label}<input type="text" name="Sname" value="$Sname" />
    {$label}<input type="text" name="Address1" value="$Address1" />
    {$label}<input type="text" name="Address2" value="$Address2" />
    {$label}<input type="text" name="Town" value="$Town" />
    {$label}<input type="text" name="County" value="$County" />
    </p>
    <p id="rightCol">
    {$label}<input type="text" name="Hphone" value="$Hphone" />
    {$label}<input type="text" name="Mphone" value="$Mphone" />
    {$label}<input type="text" name="Appliance" value="$Appliance" />
    {$label}<input type="text" name="Brand" value="$Appliance" />
    {$label}<input type="text" name="Brand" value="$Brand" />
    </p>
    <p id="centerCol">
    <input type="submit" value="Submit" />
    </p>
    </form>';
    [/PHP]

    As you can see its a form but the page comes up blank when loaded. Why? I think the arrays might written wrong. Is their any need to write them that way?

    Thanks,
    S.


Comments

  • Closed Accounts Posts: 429 ✭✭Myxomatosis


    Fixed :)
    [PHP]<?php
    $label = array( 'Fname' => 'First Name:',
    'Sname' => 'Surname:',
    'Address1' => 'Address 1:',
    'Address2' => 'Address 2:',
    'Town' => 'Town/city:',
    'County' => 'County:',
    'Hphone' => 'Phone No.:',
    'Mphone' => 'Mobile No.:',
    'Appliance' => 'Appliance Type:',
    'Brand' => 'Appliance Brand:',
    'Snumber' => 'Serial No.:');

    echo "<div id='form'>
    <form action='ENTER_LINK.php' method='POST'>
    <p id='leftCol'>
    {".$label."}<input type='text' name='Fname' value='$Fname' />
    {".$label."}<input type='text' name='Sname' value='$Sname' />
    {".$label."}<input type='text' name='Address1' value='$Address1' />
    {".$label."}<input type='text' name='Address2' value='$Address2' />
    {".$label."}<input type='text' name='Town' value='$Town' />
    {".$label."}<input type='text' name='County' value='$County' />
    </p>
    <p id='rightCol'>
    {".$label."}<input type='text' name='Hphone' value='$Hphone' />
    {".$label."}<input type='text' name='Mphone' value='$Mphone' />
    {".$label."}<input type='text' name='Appliance' value='$Appliance' />
    {".$label."}<input type='text' name='Brand' value='$Appliance' />
    {".$label."}<input type='text' name='Brand' value='$Brand' />
    </p>
    <p id='centerCol'>
    <input type='submit' value='Submit' />
    </p>
    </form>";
    ?> [/PHP]


    An array can't be echoed like this, apparently
    [PHP]echo "array";[/PHP]

    You can't include it in a string, you have to end the string, concatenate the value from the array, then concatenate the rest of the string. In PHP string concatenation is done with the dot, ie
    [PHP]echo "blah" . $blah . "blah";[/PHP]

    Also
    [PHP]<input type="text" value="$something">[/PHP]
    won't print out the value of $something, but rather just print out the word $something

    To avoid this, use single quotes
    [PHP]<input type='text' value='$something'>[/PHP]

    Also be careful when a string has quotes inside it.

    You have two options, either escape the quotation marks with a backslash, or , lets say for example, you have
    [PHP]echo "<input type='text' name="Brand" value='' />"[/PHP]

    This will give you an error because you are using double quotes inside a string which uses double quotes on the "edges".

    Use

    [PHP]echo "<input type='text' name='Brand' value='' />"[/PHP]

    or

    [PHP]echo '<input type="text" name="Brand" value="" />'[/PHP]


    EDIT:
    Oh and your page comes up blank because your server is probably configured to write errors to a log file. If you the server installed on your own computer I'd advise you to turn on error reporting to be displayed as part of the output of the php file. This will allow you easily see what line the error is on and a starting place to fix it.


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    Thanks but I've another question,
    Is there any need for the array? Should I not just right in the text? They only reason I've the array is because thats whats used in the example of my book.

    Thanks again,
    S.


  • Closed Accounts Posts: 429 ✭✭Myxomatosis


    Yeah just writing the text would be a lot easier. There's not logic reason to use an array for the labels. Your book is just probably trying to show you different ways an array can be wrote and used.


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    k,
    Thanks,
    S.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Yeah just writing the text would be a lot easier. There's not logic reason to use an array for the labels. Your book is just probably trying to show you different ways an array can be wrote and used.
    At a simple level, this is true. You should really only use variables for storing either transitive data or constants that will be used in equations.

    That said, arrays or constants are often used for storing text where multilanguage functions are in use.

    On topic, as a rule you should try to minimise the amount of HTML that you echo from PHP. It makes the whole thing neater and easier to read, and in my experience it makes both the PHP and HTML much easier to maintain and debug.

    So I would rewrite the above as:
    <?php
    $label = array( 'Fname' => 'First Name:',
            'Sname' => 'Surname:',
            'Address1' => 'Address 1:',
            'Address2' => 'Address 2:',
            'Town' => 'Town/city:',
            'County' => 'County:',        
            'Hphone' => 'Phone No.:',
            'Mphone' => 'Mobile No.:',
            'Appliance' => 'Appliance Type:',
            'Brand' => 'Appliance Brand:',
            'Snumber' => 'Serial No.:');
    ?>
    <div id='form'>
        <form action='ENTER_LINK.php' method='POST'>
            <p id='leftCol'>
                <?php echo $label['Fname'] ?><input type='text' name='Fname' value='<?php echo $Fname ?>' />
                <?php echo $label['Sname'] ?><input type='text' name='Sname' value='<?php echo $Sname ?>' />
                <?php echo $label['Address1'] ?><input type='text' name='Address1' value='<?php echo $Address1 ?>' /> 
                <?php echo $label['Address2'] ?><input type='text' name='Address2' value='<?php echo $Address2 ?>' />
                <?php echo $label['Town'] ?><input type='text' name='Town' value='<?php echo $Town ?>' />
                <?php echo $label['County'] ?><input type='text' name='County' value='<?php echo $County ?>' />
            </p>
            <p id='rightCol'>
                <?php echo $label['Hphone'] ?><input type='text' name='Hphone' value='<?php echo $Hphone ?>' />
                <?php echo $label['Mphone'] ?><input type='text' name='Mphone' value='<?php echo $Mphone ?>' />
                <?php echo $label['Appliance'] ?><input type='text' name='Appliance' value='<?php echo $Appliance ?>' />
                <?php echo $label['Brand'] ?><input type='text' name='Brand' value='<?php echo $Appliance ?>' />
                <?php echo $label['Snumber'] ?><input type='text' name='Brand' value='<?php echo $Brand ?>' />
            </p>
            <p id='centerCol'>
                <input type='submit' value='Submit' />
            </p>    
        </form>
    


  • Advertisement
  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Since the array is being used to store the input labels wouldn't it make more sense to use a loop to write the form to the screen. Then the array makes sense as you can update the page by adding new items to array without needing to write in the input boxes for the new items.

    Just a thought
    -RD


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    So instead of having Fname, have 1 and write a loop that will add up by 1 displaying the arrays? My PHP is shoddy but I think its similiar to JS so this is what I'm guessen..
    [PHP]
    $label = array( '1' => 'First Name:',
    '2' => 'Surname:',
    '3' => 'Address 1:');

    for(i=0; i!>count("$label"); i++){
    echo "$label[.i.] <input type='text' name='.i.'/>";
    if(i==5){
    echo "</p><p id='rightcol'>";
    }
    }
    [/PHP]

    Is that right? Anywhere close?


Advertisement