Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: How to insert value to textfield in PHP?

  1. #1
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to insert value to textfield in PHP?

    For the code below,how can I insert $value into the textfiled respectively?And did there any code like setEditable=false in Java to disable editing of particular textfield?Thanks...


    PHP Code:
    <?php
    include ('dbconn.cfg');// database configuration file
    $connection = @mysql_connect("localhost""root""") or die("Cannot connect to server!");

    if (isset(
    $_SESSION['gmembername']))
    {
        
    $tbl_name "member";
        
    $sql "SELECT name,telephone,address FROM $tbl_name";

        
    $result = @mysql_query($sql$connection) or die("Cannot execute query.");
        
    $numrow mysql_num_rows($result);
        if (
    $numrow != 0)
        {
            
    // fetch each record in result set
            
    for ($counter == 0$row mysql_fetch_row($result); $counter++)
            {
                
    // build table to display result
                
    print ("<h2><center>Member Profile</center></h2>");
                print (
    "<table border = '1'  cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>");
                print (
    "<tr>");
                print (
    "<th>Full Name</th>");
                print (
    "<th>Telephone</th>");
                print (
    "<th>Address</th>");
                print (
    "</tr>");
                print (
    "<tr>");

                foreach (
    $row as $key => $value)
                    print (
    "<td><input type ='text'size = '30'></td>");

                print (
    "</tr>");
                print (
    "</table>");
            }
        }
    }
    ?>

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try this for the inserting of the field.

    Code:
    <?php
    include ('dbconn.cfg');// database configuration file
    $connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");
    
    if (isset($_SESSION['gmembername']))
    {
        $tbl_name = "member";
        $sql = "SELECT name,telephone,address FROM $tbl_name";
    
        $result = @mysql_query($sql, $connection) or die("Cannot execute query.");
        $numrow = mysql_num_rows($result);
        if ($numrow != 0)
        {
            // fetch each record in result set
            for ($counter == 0; $row = mysql_fetch_row($result); $counter++)
            {
                // build table to display result
                print ("<h2><center>Member Profile</center></h2>");
                print ("<table border = '1'  cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>");
                print ("<tr>");
                print ("<th>Full Name</th>");
                print ("<th>Telephone</th>");
                print ("<th>Address</th>");
                print ("</tr>");
                print ("<tr>");
    
                foreach ($row as $key => $value)
                    print ("<td><input type ='text' value='$value' size = '30'></td>");
    
                print ("</tr>");
                print ("</table>");
            }
        }
    }
    ?>
    As for the editing of only certain fields, you can append "readonly" to the field that you don't want editable. This would look something like the following:

    Code:
    <input type="text" name="blah" value="blah blah" readonly>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by thetestingsite View Post
    Try this for the inserting of the field.

    Code:
    <?php
    include ('dbconn.cfg');// database configuration file
    $connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");
    
    if (isset($_SESSION['gmembername']))
    {
        $tbl_name = "member";
        $sql = "SELECT name,telephone,address FROM $tbl_name";
    
        $result = @mysql_query($sql, $connection) or die("Cannot execute query.");
        $numrow = mysql_num_rows($result);
        if ($numrow != 0)
        {
            // fetch each record in result set
            for ($counter == 0; $row = mysql_fetch_row($result); $counter++)
            {
                // build table to display result
                print ("<h2><center>Member Profile</center></h2>");
                print ("<table border = '1'  cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>");
                print ("<tr>");
                print ("<th>Full Name</th>");
                print ("<th>Telephone</th>");
                print ("<th>Address</th>");
                print ("</tr>");
                print ("<tr>");
    
                foreach ($row as $key => $value)
                    print ("<td><input type ='text' value='$value' size = '30'></td>");
    
                print ("</tr>");
                print ("</table>");
            }
        }
    }
    ?>
    As for the editing of only certain fields, you can append "readonly" to the field that you don't want editable. This would look something like the following:

    Code:
    <input type="text" name="blah" value="blah blah" readonly>
    Hope this helps.

    oh..I don't realize existence of this feature,thanks...

  4. #4
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    If I change address field to textarea then how each $value being fixed to its location?The following code seems lost the textfield and textarea...thanks..

    PHP Code:
    foreach ($row as $key => $value)
                    if(
    $row == && $row == 1)
                    print (
    "<td><input type ='text' size = '30' value = '$value'readonly></td>");
                    else if(
    $row == 2)
                    print(
    "<td><textarea rows = '3' value = '$value'></textarea></td>"); 

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    For textareas, you place the default value (what you want displayed) in between the <textare> tags like so:

    Code:
    foreach ($row as $key => $value)
                    if($row == 0 && $row == 1)
                    print ("<td><input type ='text' size = '30' value = '$value' readonly></td>");
                    else if($row == 2)
                    print("<td><textarea rows = '3'>$value</textarea></td>");
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by thetestingsite View Post
    For textareas, you place the default value (what you want displayed) in between the <textare> tags like so:

    Code:
    foreach ($row as $key => $value)
                    if($row == 0 && $row == 1)
                    print ("<td><input type ='text' size = '30' value = '$value' readonly></td>");
                    else if($row == 2)
                    print("<td><textarea rows = '3'>$value</textarea></td>");
    Hope this helps.

    Unfortunately,y textarea and textfield still gone,this is the table structure:

    PHP Code:
     print ("<h2><center>Member Profile</center></h2>");
                print (
    "<table border = '1'  cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>");
                print (
    "<tr>");
                print (
    "<th>Full Name</th>");
                print (
    "<th>Telephone</th>");
                print (
    "<th>Address</th>");
                print (
    "</tr>");
                print (
    "<tr>");

                foreach (
    $row as $key => $value)
                    if(
    $row == && $row == 1)
                    print (
    "<td><input type ='text' size = '30' value = '$value' readonly></td>");
                    else if(
    $row == 2)
                    print(
    "<td><textarea rows = '3'>$value</textarea></td>");
                    
                print (
    "</tr>");
                print (
    "</table>");
                
                print (
    "<table>");
                print (
    "<tr>");
                print (
    "<td>&nbsp;</td>");
                print (
    "<td width='4%'>&nbsp;</td>");
                print (
    "<td></tr>");

                print (
    "<tr>");
                print (
    "<td>&nbsp;</td>");
                print (
    "<td width='4%'>&nbsp;</td>");
                print (
    "<td><input type='submit' name='submit' value='Upgrade'></td>");
                print (
    "</table>"); 

  7. #7
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    I would break out of PHP, first of all, for just HTML:

    PHP Code:
    <?php  ?>
    <h2><center>Member Profile</center></h2>
    <!-- rest of code before foreach statement
    <?php 

                
    foreach ($row as $key => $value)
                    if(
    $row == && $row == 1)
                    print (
    "<td><input type ='text' size = '30' value = '$value' readonly></td>");
                    else if(
    $row == 2)
                    print(
    "<td><textarea rows = '3'>$value</textarea></td>"); 
    ?>

    </tr>
    </td>
    <!-- rest of html here -->
    Now, for the textarea, it's better to write the readonly like, readonly="readonly". What you have now will work, though you might want to replace it with something more valid.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  8. #8
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I think I have confusing with html and php clauses...
    What went wrong in my code?Thanks...


    PHP Code:
    <?php
    include ('dbconn.cfg');// database configuration file
    $connection = @mysql_connect("localhost""root""") or die("Cannot connect to server!");
    ?>
                // build table to display result
                <html>
                <title></title>
                <head>
                <body>
                <h2><center>Member Profile</center></h2>
                <table border = '1'  cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>
                <tr>
                <th>Full Name</th>
                <th>Telephone</th>
                <th>Address</th>
                </tr>
                <tr>
                
                <?

    if (isset($_SESSION['gmembername']))
    {
        
    $tbl_name "member";
        
    $sql "SELECT name,telephone,address FROM $tbl_name";

        
    $result = @mysql_query($sql$connection) or die("Cannot execute query.");
        
    $numrow mysql_num_rows($result);
        
        if(
    $numrow != 0)
        {
    // fetch each record in result set
            
    for ($counter == 0$row mysql_fetch_row($result); $counter++)
          {
                  foreach (
    $row as $key => $value)
                   if (
    $row == && $row == 1)
                     print (
    "<td><input type ='text' size = '30' value = '$value' readonly></td>");
               else
                  if (
    $row == 2)
                     print (
    "<td><textarea rows = '3'>$value</textarea></td>");
          }
        }
    }
    ?>
     
                 </tr>
                </table>
                <table>
                <tr>
                <td>&nbsp;</td>
                <td width='4%'>&nbsp;</td>
                <td></tr>
                <tr>
                <td>&nbsp;</td>
                <td width='4%'>&nbsp;</td>
                <td><input type='submit' name='submit' value='Upgrade'></td>
                </table>
                </body>
                </head>
                </html>


    Quote Originally Posted by alexjewell View Post
    I would break out of PHP, first of all, for just HTML:

    PHP Code:
    <?php  ?>
    <h2><center>Member Profile</center></h2>
    <!-- rest of code before foreach statement
    <?php 

                
    foreach ($row as $key => $value)
                    if(
    $row == && $row == 1)
                    print (
    "<td><input type ='text' size = '30' value = '$value' readonly></td>");
                    else if(
    $row == 2)
                    print(
    "<td><textarea rows = '3'>$value</textarea></td>"); 
    ?>

    </tr>
    </td>
    <!-- rest of html here -->
    Now, for the textarea, it's better to write the readonly like, readonly="readonly". What you have now will work, though you might want to replace it with something more valid.

  9. #9
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try this instead (my changes are highlighted):

    Code:
    <?php
    include ('dbconn.cfg');// database configuration file
    $connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");
    ?>
                // build table to display result
                <html>
                <title></title>
                <head>
                <body>
                <h2><center>Member Profile</center></h2>
                <table border = '1'  cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>
                <tr>
                <th>Full Name</th>
                <th>Telephone</th>
                <th>Address</th>
                </tr>
                <tr>
                
                <?
    
    if (isset($_SESSION['gmembername']))
    {
        $tbl_name = "member";
        $sql = "SELECT name,telephone,address FROM $tbl_name";
    
        $result = @mysql_query($sql, $connection) or die("Cannot execute query.");
        $numrow = mysql_num_rows($result);
        
        if($numrow != 0)
        {// fetch each record in result set
            for ($counter = 0; $row = mysql_fetch_row($result); $counter++)
          {
                  foreach ($row as $key => $value)
                   if ($key == 0 && $key == 1)
    ?>
    <td><input type="text" size="30" value="<?php echo $value;?>" readonly="readonly"></td>
    <?php
               else
                  if ($row == 2)
    ?>
    <td><textarea rows="3"> <?php echo $value; ?> </textarea></td>
    <?php
          }
        }
    }
    ?>
     
                 </tr>
                </table>
                <table>
                <tr>
                <td>&nbsp;</td>
                <td width='4&#37;'>&nbsp;</td>
                <td></tr>
                <tr>
                <td>&nbsp;</td>
                <td width='4%'>&nbsp;</td>
                <td><input type='submit' name='submit' value='Upgrade'></td>
                </table>
                </body>
                </head>
                </html>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  10. #10
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your help.However it showed syntax error of unexpected T_ELSE in line of else statement.


    Quote Originally Posted by thetestingsite View Post
    Try this instead (my changes are highlighted):

    Code:
    <?php
    include ('dbconn.cfg');// database configuration file
    $connection = @mysql_connect("localhost", "root", "") or die("Cannot connect to server!");
    ?>
                // build table to display result
                <html>
                <title></title>
                <head>
                <body>
                <h2><center>Member Profile</center></h2>
                <table border = '1'  cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>
                <tr>
                <th>Full Name</th>
                <th>Telephone</th>
                <th>Address</th>
                </tr>
                <tr>
                
                <?
    
    if (isset($_SESSION['gmembername']))
    {
        $tbl_name = "member";
        $sql = "SELECT name,telephone,address FROM $tbl_name";
    
        $result = @mysql_query($sql, $connection) or die("Cannot execute query.");
        $numrow = mysql_num_rows($result);
        
        if($numrow != 0)
        {// fetch each record in result set
            for ($counter = 0; $row = mysql_fetch_row($result); $counter++)
          {
                  foreach ($row as $key => $value)
                   if ($key == 0 && $key == 1)
    ?>
    <td><input type="text" size="30" value="<?php echo $value;?>" readonly="readonly"></td>
    <?php
               else
                  if ($row == 2)
    ?>
    <td><textarea rows="3"> <?php echo $value; ?> </textarea></td>
    <?php
          }
        }
    }
    ?>
     
                 </tr>
                </table>
                <table>
                <tr>
                <td>&nbsp;</td>
                <td width='4%'>&nbsp;</td>
                <td></tr>
                <tr>
                <td>&nbsp;</td>
                <td width='4%'>&nbsp;</td>
                <td><input type='submit' name='submit' value='Upgrade'></td>
                </table>
                </body>
                </head>
                </html>
    Hope this helps.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •