Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Oct 2005
    Location
    Newberg, Oregon
    Posts
    252
    Thanks
    1
    Thanked 0 Times in 0 Posts

    problem with file_put_contents on a form

    Hey all,

    I am basically trying to set up a few forms, that when clicked through, add a number to a file, so that when you log in to an admin section, you can see how many people have clicked through the form. I've gotten it to work in the past, and I am using the same exact form, but it's not working now. Here's the code.

    index.php
    Code:
    <form method="post" action="newsletter.php">
     <div class="contentWrapper3">
        <div class="content">
            <img class="borderTL" src="images/borderTL.gif" alt="&nbsp;" width="14" height="14" />
            <img class="borderTR" src="images/borderTR.gif" alt="&nbsp;" width="14" height="14" />
    <h1 class="displaynone">Sign Up for our Newsletter!</h1>
    	<img src="images/newsletter.jpg" alt="Sign up for our Newsletter" class="margintop" />
    	<p>Sign up for our monthly newsletter and get learn about the hottest opportunities in local real estate, as well as a chance to win exciting prizes!! </p>
    	<label>Name:</label>
    	<input name="name" size="20" type="text" /><br />
    	<label>Email:</label>
    	<input name="email" size="20" type="text" /><br />
    	<span><input value="Submit" type="submit" class="stuffs" name="newsletter" /></span>
            <!-- IE5/win puts the margin-bottom of the content div's final element
                 OUTSIDE the containing box (div.content), instead of putting it inside
                 the containing box's edge. So it needs this spacer. -->
            <div class="roundedCornerSpacer">&nbsp;</div>
        </div><!-- end of div.content -->
        <div class="bottomCorners">
            <img class="borderBL" src="images/borderBL.gif" alt="&nbsp;" width="14" height="14" />
            <img class="borderBR" src="images/borderBR.gif" alt="&nbsp;" width="14" height="14" />
        </div>
    </div><!-- end of div.contentWrapper -->
    </form><!--End of right bottom area-->
    newsletter.php
    PHP Code:
    <?PHP include "includes/header.php"?>
    <!--Content Divider-->
    <div class="contentWrapper">
        <div class="content">
            <img class="borderTL" src="images/borderTL.gif" alt="&nbsp;" width="14" height="14" />
            <img class="borderTR" src="images/borderTR.gif" alt="&nbsp;" width="14" height="14" />
    <?php
            
    function file_put_contents($filename$data)
    {
      if( 
    $file fopen($filename'w') )
      {
        
    $bytes fwrite($fileis_array($data) ? implode(''$data) : $data);
        
    fclose($file); return $bytes// return the number of bytes written to the file
      
    }
    }  
                        if(isset(
    $_POST['newsletter'])) //note, add name="newsletter" to the submit button
    {
        
    file_put_contents('newsletter.dat', @file_get_contents('newsletter.dat') + 1);
    }  
    $to       "Rick Talbott <justinsainton@gmail.com>"// change to your email address
    $name     $_POST['name'];
    $email    $_POST['email'];
    $d        date('l dS \of F Y h:i:s A');
    $sub      "From RickTalbott.com";
    $headers  "From: $name <$email>\n";  
    $headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
    $mes      "Subject: Sign Me up for your newsletter! \n";
    $mes     .= "Name: ".$name."\n";
    $mes     .= 'Email: '.$email."\n";
    $mes     .= 'Date & Time: '.$d;

    if (empty(
    $name) || empty($email))
    {
         echo 
    "   <h2>Sorry all fields are required.</h2>";
    }
    elseif(!
    ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$email)) {
         print 
    "   <h2>Sorry the email address you entered looks like it's invalid.</h2>";
    }
    else
    {
         
    mail($to$sub$mes$headers);
         print 
    "   <h2>Thank you, ".$name.", for contacting us.<br>We will get back to you as soon as possible</h2>"
    }
    ?>
    <?PHP 
    include "includes/footer.php"?>
    admin.php
    PHP Code:
    <?php

    // Define your username and password
    $username "ricktalbott";
    $password "rick";

    if (
    $_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {

    ?>
    <?PHP 
    include "includes/header.php"?>
    <!--Content Divider-->
    <div class="contentWrapper">
        <div class="content">
            <img class="borderTL" src="images/borderTL.gif" alt="&nbsp;" width="14" height="14" />
            <img class="borderTR" src="images/borderTR.gif" alt="&nbsp;" width="14" height="14" />

    <h2>Login</h2>

    <form id="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <div id="contactform">
      <label>Username:</label>
        <input type="text" title="Enter your Username" name="txtUsername" /><br />

        <label>Password:</label>
        <input type="password" title="Enter your password" name="txtPassword" /><br />

        <input type="submit" name="Submit" value="Login" />
    </div>
    </form>
    <?PHP include "includes/footer.php"?>
    <?php

    }
    else {

    ?>
    <?php
    $newsletter 
    file_get_contents('newsletter.dat');
    ?>
    <?php
    $prop 
    file_get_contents('prop.dat'); 
    ?>
    <?php
    $cma 
    file_get_contents('cma.dat'); 
    ?>
    <?php
    $search 
    file_get_contents('search.dat'); 
    ?>
    <?php
    $feature 
    file_get_contents('feature.dat');   
    ?>
    <?PHP 
    include "includes/header.php"?>
    <!--Content Divider-->
    <div class="contentWrapper">
        <div class="content">
            <img class="borderTL" src="images/borderTL.gif" alt="&nbsp;" width="14" height="14" />
            <img class="borderTR" src="images/borderTR.gif" alt="&nbsp;" width="14" height="14" />
    <h1 id="contact">Welcome to your administrative page, Rick.</h1>
    <h2><?php print $newsletter ?> People have signed up for your newsletter</h2>
    <h2><?php print $prop ?> People have registered for property tracking from this site.</h2>
    <h2><?php print $cma ?> People have people have requested a CMA</h2>
    <h2>Based on the search listings link, You have <?php print $search ?> leads</h2>
    <h2>Based on the featured listings link, You have <?php print $feature ?> leads</h2>
    <?PHP include "includes/footer.php"?>
    <?php

    }

    ?>
    So, basically the newsletter.dat file is defaulted to zero, and in the admin section, it shows 0. So I don't think it's necessarily a problem in the admin.php file. But, when i submit the form a few times, it doesn't post anything to the dat file. I guess that's the main problem.

    Also, I am wanting to implement this same idea, but with clicking through a link instead of a form. Any ideas on how to accomplish that?

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Newberg, Oregon
    Posts
    252
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well, I got help with the first issue on the SitePoint forums. I got a little help with the issue over there at this link:

    http://www.sitepoint.com/forums/showthread.php?t=393145

    Rather than reposting everything, maybe you could check out that link and see if you can't help me out with the second issue. Thanks!

  • #3
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    About your links problem, try to change
    PHP Code:
    onchange="javascript: form.submit()" 
    to
    PHP Code:
    onchange="this.form.submit()" 


  •  

    Posting Permissions

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