Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-31-2011, 11:46 PM   PM User | #1
ARCLite Studio
New Coder

 
Join Date: Sep 2009
Posts: 73
Thanks: 5
Thanked 9 Times in 9 Posts
ARCLite Studio is an unknown quantity at this point
Question Why does script create 2 records?

I don't use PHP if I can help it but in some cases I have too, in this case I needed a form to capture some information, wrap the results in some additional HTML and Microformats coding and then append it to a file so i could use PHP Include.

This took a lot of searching and testing for me to pull it off (Don't know PHP very well), but anyways here is the problem - When I submit the form it makes 2 records in the file, one that is blank (no form data) and the second record which has the proper information. Other than creating two records it works properly, what do i need to change for this to create only the record with the form data?

Note: My first thoughts were this:action="<?php echo $PHP_SELF;?>" was the cause of the duplication, but if that is the cause i don't know what to change it to for it to work properly.

PHP Code:
<?php
$Rname 
$_POST["rname"];
$Rate $_POST["rating"];
$Title $_POST["title"];
$Sumr $_POST["summary"];
$mnth $_POST["month"];
$day $_POST["day"];
$year $_POST["year"];
?>

<form id="form1" method="post" action="<?php echo $PHP_SELF;?>">
  Reviewers Name:<input name="rname" type="text" size="30" maxlength="30">
  <br>
  <div xmlns="http://www.w3.org/1999/xhtml" class="field">
          <label for="rating">rating</label>
          <select id="rating" name="rating">
            <option/>
            <option label="★" value="1">1</option>
            <option label="★★" value="2">2</option>
            <option label="★★★" value="3">3</option>

            <option label="★★★★" value="4">4</option>
            <option label="★★★★★" value="5">5</option>
          </select>
          <label id="summary-label" for="title">Title</label>
          <input type="text" id="title" name="title"/><br>
          <label id="summary-label" for="title">Review</label>
          <textarea rows="10" cols="30" name="summary" id="summary"></textarea><br>
          <label for="review-date-month">review date</label>
          <select id="review-date-month" name="month">
            <option value="01">Jan</option>
            <option value="02">Feb</option>
            <option value="03">Mar</option>
            <option value="04">Apr</option>

            <option value="05">May</option>
            <option value="06">Jun</option>
            <option value="07">Jul</option>
            <option value="08">Aug</option>
            <option value="09">Sep</option>
            <option value="10">Oct</option>

            <option value="11">Nov</option>
            <option value="12">Dec</option>
          </select>
          <select id="review-date-day" name="day">
            <option value="01">1</option>
            <option value="02">2</option>
            <option value="03">3</option>
            <option value="04">4</option>
            <option value="05">5</option>
            <option value="06">6</option>
            <option value="07">7</option>
            <option value="08">8</option>
            <option value="09">9</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option>
            <option value="14">14</option>
            <option value="15">15</option>
            <option value="16">16</option>
            <option value="17">17</option>
            <option value="18">18</option>
            <option value="19">19</option>
            <option value="20">20</option>
            <option value="21">21</option>
            <option value="22">22</option>
            <option value="23">23</option>
            <option value="24">24</option>
            <option value="25">25</option>
            <option value="26">26</option>
            <option value="27">27</option>
            <option value="28">28</option>
            <option value="29">29</option>
            <option value="30">30</option>
            <option value="31">31</option>
          </select>
          <select id="review-date-year" name="year">
            <option value="2004">2004</option>
            <option value="2005">2005</option>
            <option value="2006">2006</option>
            <option value="2007">2007</option>
            <option value="2008">2008</option>
            <option value="2009">2008</option>
            <option value="2010">2008</option>
            <option value="2011">2008</option>
            <option value="2012">2008</option>
          </select>
        </div>

  <input type="submit" value="submit" >
</form>


<?php 

$myFile 
"test.php";
$fh fopen($myFile'a') or die("can't open file");
$stringData "
<div class='tstyle'><img src='/images/testimonial-images/comingSoon.png' width='263' height='210' align='left' />
<div id='hreview' class='hreview'>
<h2 class='summary'>$Title</h2>
<abbr class='dtreviewed bold'>$mnth-$day-$year</abbr> by <span class='reviewer vcard'><span class='fn cgreen'>$Rname</span></span>
<span style='display: none;' class='type'>business</span><div class='item vcard hide'><span class='fn org'>Organization Here</span><div class='tel'>(###) ###-####</div><div class='adr'><div class='street-address'>Address Here</div><span class='locality'>City</span><span class='region'> ST</span>, <span class='postal-code'> ZIP</span></div></div>
<blockquote class='description'><p><abbr class='rating hide' title='$Rate'> $Rate out of 5 Stars</abbr>$Sumr</p></blockquote></div>
<p>&nbsp;</p><div class='fb-like' data-href='http://URL' data-send='false' data-width='335' data-show-faces='false' data-action='recommend'></div> </div>

<br />



"
;
fwrite($fh$stringData);
fclose($fh);


?>

Last edited by ARCLite Studio; 01-01-2012 at 02:17 AM..
ARCLite Studio is offline   Reply With Quote
Old 01-01-2012, 12:08 AM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,519
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Of course it will do it twice - once when you call the script (and there is no input) and then once when you submit it:

Try this:
PHP Code:
<?php
$Rname 
$_POST["rname"];
$Rate $_POST["rating"];
$Title $_POST["title"];
$Sumr $_POST["summary"];
$mnth $_POST["month"];
$day $_POST["day"];
$year $_POST["year"];
?>

<form id="form1" method="post" action="<?php echo $PHP_SELF;?>">
  Reviewers Name:<input name="rname" type="text" size="30" maxlength="30">
  <br>
  <div xmlns="http://www.w3.org/1999/xhtml" class="field">
          <label for="rating">rating</label>
          <select id="rating" name="rating">
            <option/>
            <option label="★" value="1">1</option>
            <option label="★★" value="2">2</option>
            <option label="★★★" value="3">3</option>

            <option label="★★★★" value="4">4</option>
            <option label="★★★★★" value="5">5</option>
          </select>
          <label id="summary-label" for="title">Title</label>
          <input type="text" id="title" name="title"/><br>
          <label id="summary-label" for="title">Review</label>
          <textarea rows="10" cols="30" name="summary" id="summary"></textarea><br>
          <label for="review-date-month">review date</label>
          <select id="review-date-month" name="month">
            <option value="01">Jan</option>
            <option value="02">Feb</option>
            <option value="03">Mar</option>
            <option value="04">Apr</option>

            <option value="05">May</option>
            <option value="06">Jun</option>
            <option value="07">Jul</option>
            <option value="08">Aug</option>
            <option value="09">Sep</option>
            <option value="10">Oct</option>

            <option value="11">Nov</option>
            <option value="12">Dec</option>
          </select>
          <select id="review-date-day" name="day">
            <option value="01">1</option>
            <option value="02">2</option>
            <option value="03">3</option>
            <option value="04">4</option>
            <option value="05">5</option>
            <option value="06">6</option>
            <option value="07">7</option>
            <option value="08">8</option>
            <option value="09">9</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option>
            <option value="14">14</option>
            <option value="15">15</option>
            <option value="16">16</option>
            <option value="17">17</option>
            <option value="18">18</option>
            <option value="19">19</option>
            <option value="20">20</option>
            <option value="21">21</option>
            <option value="22">22</option>
            <option value="23">23</option>
            <option value="24">24</option>
            <option value="25">25</option>
            <option value="26">26</option>
            <option value="27">27</option>
            <option value="28">28</option>
            <option value="29">29</option>
            <option value="30">30</option>
            <option value="31">31</option>
          </select>
          <select id="review-date-year" name="year">
            <option value="2004">2004</option>
            <option value="2005">2005</option>
            <option value="2006">2006</option>
            <option value="2007">2007</option>
            <option value="2008">2008</option>
            <option value="2009">2008</option>
            <option value="2010">2008</option>
            <option value="2011">2008</option>
            <option value="2012">2008</option>
          </select>
        </div>

  <input type="submit" value="submit" >
</form>


<?php 
if (!empty($_POST))
{
$myFile "test.php";
$fh fopen($myFile'a') or die("can't open file");
$stringData "
<div class='tstyle'><img src='/images/testimonial-images/comingSoon.png' width='263' height='210' align='left' />
<div id='hreview' class='hreview'>
<h2 class='summary'>$Title</h2>
<abbr class='dtreviewed bold'>$mnth-$day-$year</abbr> by <span class='reviewer vcard'><span class='fn cgreen'>$Rname</span></span>
<span style='display: none;' class='type'>business</span><div class='item vcard hide'><span class='fn org'>Organization Here</span><div class='tel'>(###) ###-####</div><div class='adr'><div class='street-address'>Address Here</div><span class='locality'>City</span><span class='region'> ST</span>, <span class='postal-code'> ZIP</span></div></div>
<blockquote class='description'><p><abbr class='rating hide' title='$Rate'> $Rate out of 5 Stars</abbr>$Sumr</p></blockquote></div>
<p>&nbsp;</p><div class='fb-like' data-href='http://URL' data-send='false' data-width='335' data-show-faces='false' data-action='recommend'></div> </div>

<br />



"
;
fwrite($fh$stringData);
fclose($fh);
}
?>
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
ARCLite Studio (01-01-2012)
Old 01-01-2012, 02:16 AM   PM User | #3
ARCLite Studio
New Coder

 
Join Date: Sep 2009
Posts: 73
Thanks: 5
Thanked 9 Times in 9 Posts
ARCLite Studio is an unknown quantity at this point
Thumbs up

Quote:
Originally Posted by tangoforce View Post
Of course it will do it twice
<---- Haha, you say "of course" like non PHP programmer me should have known that... lol.


In any case, thank you, If I'm understanding the change correctly it is essentially saying "if POST not empty then continue else die"


I could be wrong, but either way it fixed the problem thanks TangoForce
ARCLite Studio is offline   Reply With Quote
Old 01-01-2012, 02:22 AM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,519
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by ARCLite Studio View Post
In any case, thank you, If I'm understanding the change correctly it is essentially saying "if POST not empty then continue else die"
Correct
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Tags
fopen, php, php append

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:27 PM.


Advertisement
Log in to turn off these ads.