SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question newbie question - displaying form variables with form action=""

    Hi all. I am new to PHP/MySQL and am working my way through Build your own database driven website using PHP and MSQL 4th edition. I have just finished chapter 4 and am implementing what I have learnt with a basic site to submit data through a form to a database and display it (different to the example in the book). I have written code that first checks for values (domain and author) posted by a form using $_REQUEST, second lists all the values stored in the database and third includes the actual form for submitting new values (domain and author). The form has action="" and so is submitting to the current page. My thinking with this is that when the form is submitted the page will be reloaded and the first part of the code will capture the variables and the second will display them. But when I submit new values with the form the new values are not listed on the page until the page is manually reloaded (which then causes the values to be added to the database twice). Does submitting a form with action="" not cause the current page to be reloaded (and therefore display the values on the page)?

    I've included my relevant code below. Thanks very much for any advice.

    Code:
    if (isset($_REQUEST['domain']))  // check to see if form has been submitted
    	{
    		
    	$domain = mysqli_real_escape_string($link, $_REQUEST['domain']);
    	$author = mysqli_real_escape_string($link, $_REQUEST['author']);
    	$sql = 'INSERT INTO domainsuggestions SET
    	domain="' . $domain .'",
    	author="' . $author . '",
    	submitdate=CURDATE()';
    	
    	if (!mysqli_query($link, $sql))  // insert domain suggestion into table
    		{
    			$output = 'Unable to add suggestion to table. Error '. mysqli_error($link);
    			echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
    		}
    	}
    	
    while ($row = mysqli_fetch_array($result))
    {
    $domains[] = array('domain' => $row['domain'], 'author' => $row['author'], 'id' => $row['id']);
    }
    
    <table>
        	<tr>
                <td>ID</td>
                <td>Domain Suggestions</td>
                <td>Author</td>
            </tr>	
    <?php foreach ($domains as $item) { ?>
    	<tr>
    	    <td><?php echo htmlspecialchars($item['id'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($item['domain'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($item['author'], ENT_QUOTES, 'UTF-8'); ?></td>
            </tr>
    <?php } ?>
    </table>
    
    <form method="post" action="">
    <div>
    	<label for="domain name">Suggest a Domain:</label> <input type="text" name="domain" /><br />
    	<label for="author name">Your Name:</label> <input type="text" name="author" /><br />
        <input type="submit" />
    </div>
    </form>

  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I modified your script a bit. Take a look and if this helps:

    PHP Code:
    <?php
       
    include("domain_config.php");
       
    $link = @mysqli_connect($db_host$db_username$db_password,$db_name);

       if(
    $_REQUEST['domain'] && $_REQUEST['author'])
       {
           
    $domain mysqli_real_escape_string($link$_REQUEST['domain']);
           
    $author mysqli_real_escape_string($link$_REQUEST['author']);
           
    $sql "INSERT INTO domainsuggestions SET domain='$domain',author='$author',submitdate=CURDATE()";
           if (!
    mysqli_query($link$sql))  // insert domain suggestion into table
           
    {
               
    $output 'Unable to add suggestion to table. Error 'mysqli_error($link);
               echo 
    htmlspecialchars($outputENT_QUOTES'UTF-8');
           }
       }
       
    $result mysqli_query($link"SELECT * FROM domainsuggestions");
    ?>
    <table>
       <tr>
          <td>ID</td>
          <td>Domain Suggestions</td>
          <td>Author</td>
          <td>Date</td>
       </tr>    
       <?php while($row mysqli_fetch_array($resultMYSQLI_ASSOC)) { ?>
       <tr>
          <td><?php echo $row['id']; ?></td>
          <td><?php echo $row['domain']; ?></td>
          <td><?php echo $row['author']; ?></td>
          <td><?php echo $row['submitdate']; ?></td>
       </tr>
       <?php ?>
    </table>
    <h3>Your suggestion:</h3>
    <form method="post" action="">
    <div>
        <label for="domain name">Suggest a Domain:</label> <input
        type="text" name="domain" value="<?php echo $_REQUEST['domain']; ?>"/><br />
        <label for="author name">Your Name:</label> <input
        type="text" name="author" value = "<?php echo $_REQUEST['author']; ?>"/><br />
        <input type="submit" />
    </div>
    </form>
    You can test it out here:
    http://www.itsawebthing.org/test/domain.php

  3. #3
    SitePoint Member
    Join Date
    May 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks - the main thing I had wrong was having the code to pull the info out of the db before the code to add the new suggestion. Pretty simple!

    PS. What's the vbcode to insert PHP parsed code into posts, as opposed to general code?

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Click on Go Advanced and you will see a new editor with more options and one of which will allow you to input PHP code. I'm new here to so I'm still finding my way around.

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    [ PHP ] code here [ /PHP ] (no spaces)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •