www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Beginner with some questions and issues with php

  1. #1
    Join Date
    Feb 2014
    Posts
    23

    Beginner with some questions and issues with php

    I am building a website that has a contact form which is written in php and ajax, I have renamed contact.html to contact.php and now when i view my page in chrome instead of seeing the website I see all of my code. It works in firefox however.


    Code:
    <?php session_start() ?>
    
    <!doctype html>
    <html lang="en-US">
    <head>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
      <title>The Salvation Army of West Chester</title>
      <meta name="author" content="Michael Stypko">
      <link rel="shortcut icon" href="http://spyrestudios.com/favicon.ico">
      <link rel="icon" href="http://spyrestudios.com/favicon.ico">
      <link rel="stylesheet" type="text/css" media="all" href="styles.css">
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
     <script>
      function initialize() {
        
    	var myLatlng = new google.maps.LatLng(39.960478,-75.603013);
        var map_options = {
          center: myLatlng,
          zoom: 19,
    	  mapTypeControl: true,
    	  mapTypeControlOptions: {
          style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
        },
        zoomControl: true,
        zoomControlOptions: {
          style: google.maps.ZoomControlStyle.LARGE
        },
          mapTypeId: google.maps.MapTypeId.ROADMAP
    	  
    	  
    	}
        var map = new google.maps.Map(document.getElementById('map_canvas'), map_options, marker);
    	
    	var marker = new google.maps.Marker({
          position: myLatlng,
    	  map: map
    	  });
      }
     
      google.maps.event.addDomListener(window, 'load', initialize);
    
    </script>
    
    </head>
    
    <body>
    <div id="main">
    
    <image src="salvationarmy2.png" id="banner"></>
      <div id="w">
        <nav>
          <ul id="ddmenu">
            <li><a href="index.html">Homepage</a></li>
            <li><a href="#">About</a>
              <ul>
                <li><a href="#">Our Mission</a></li>
                <li><a href="#">The Team</a></li>
                <li><a href="#">History</a></li>
              </ul>
            </li>
            <li><a href="#">Products</a>
              <ul>
                <li><a href="#">Pre-Built Logos</a></li>
                <li><a href="#">Web Templates</a></li>
                <li><a href="#">Icon Sets</a></li>
                <li><a href="#">jQuery Plugins</a></li>
                <li><a href="#">Internet Marketing</a></li>
              </ul>
            </li>
            <li><a href="#">International</a>
              <ul>
                <li><a href="#">China</a></li>
                <li><a href="#">Japan</a></li>
                <li><a href="#">Canada</a></li>
                <li><a href="#">Australia</a></li>
                <li><a href="#">South America</a></li>
              </ul>
            </li>
            <li><a href="#">Contact Us</a></li>
          </ul>
        </nav>
       </div>
       <div id="innermain">
       <div id="contact-form" class="clearfix">
        <h1>Get In Touch!</h1>
        <h2>Fill out our super swanky HTML5 contact form below to get in touch with us! Please provide as much information as possible for us to help you with your enquiry :)</h2>
    	<?php
    //init variables
    $cf = array();
    $sr = false;
     
    if(isset($_SESSION['cf_returndata'])){
        $cf = $_SESSION['cf_returndata'];
        $sr = true;
    }
    ?>
        <ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">
        <li id="info">There were some problems with your form submission:</li>
        <?php
        if(isset($cf['errors']) && count($cf['errors']) > 0) :
            foreach($cf['errors'] as $error) :
        ?>
        <li><?php echo $error ?></li>
        <?php
            endforeach;
        endif;
        ?>
    </ul>
    <p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thanks for your message! We will get back to you ASAP!</p>
        <form method="post" action="process.php">
            <label for="name">Name: <span class="required">*</span></label>
            <input type="text" id="name" name="name" value="" placeholder="John Doe" required="required" autofocus="autofocus" />
             
            <label for="email">Email Address: <span class="required">*</span></label>
            <input type="email" id="email" name="email" value="" placeholder="johndoe@example.com" script type="text/javascript" required ="required">
    /* <![CDATA[ */
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
    /* ]]> */
    
             
            <label for="telephone">Telephone: </label>
            <input type="tel" id="telephone" name="telephone" value="" />
             
            <label for="enquiry">Enquiry: </label>
            <select id="enquiry" name="enquiry">
                <option value="General">General</option>
                <option value="Housing">Housing</option>
                <option value="Volunteering">Volunteering</option>
    			<option value="Donations">Donations</option>
            </select>
             
            <label for="message">Message: <span class="required">*</span></label>
            <textarea id="message" name="message" placeholder="Your message must be greater than 20 characters" required="required" data-minlength="20"></textarea>
             
            <span id="loading"></span>
            <input type="submit" value="Submit" id="submit-button" />
            <p id="req-field-desc"><span class="required">*</span> indicates a required field</p>
        </form>
    </div>
    		
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       
       </div>
       <div id="map_canvas">
       </div>
       <div id="info">
       <li>The Salvation Army of West Chester</li>
       <li>(610) 696-8746</li>
       <li>101 East Market Street</li>
       <li>West Chester, PA 19382</li>
       </div>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
       <br>
      </div>
     
      
    <script type="text/javascript">
    $(document).ready(function(){
      $('li:has(ul) > a').on('click', function (e) {
    	e.preventDefault();
     });
        
      $('#ddmenu li').hover(function () {
         clearTimeout($.data(this,'timer'));
         $('ul',this).stop(true,true).slideDown(200);
      }, function () {
        $.data(this,'timer', setTimeout($.proxy(function() {
          $('ul',this).stop(true,true).slideUp(200);
        }, this), 100));
      });
    
    });
    </script>
    
    
    
    </body>
    </html>
    that is the code I have thusfar, sorry for posting it all but I cant pinpoint the issue.

    My second question is, I am trying to send the form data to my email address, and when I go to submit the form I am taken to my php code. Here is the php code I am using to submit the form I have created. I understand that I have to change the email addresses in the headers and mail section of the script, but I still dont think I shoudl be redirected to a page showing my php code, right? Im in a little over my head here.

    Code:
    //send email if all is ok
        if($formok){
            $headers = "From: info@example.com<script type="text/javascript">
    /* <![CDATA[ */
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
    /* ]]> */
    </script>" . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
             
            $emailbody = "<p>You have received a new message from the enquiries form on your website.</p>
                          <p><strong>Name: </strong> {$name} </p>
                          <p><strong>Email Address: </strong> {$email} </p>
                          <p><strong>Telephone: </strong> {$telephone} </p>
                          <p><strong>Enquiry: </strong> {$enquiry} </p>
                          <p><strong>Message: </strong> {$message} </p>
                          <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";
             
            mail("enquiries@example.com<script type="text/javascript">
    /* <![CDATA[ */
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
    /* ]]> */
    </script>","New Enquiry",$emailbody,$headers);
             
        }

  2. #2
    Join Date
    Feb 2014
    Posts
    23
    ok, ive fixed a lot of the problems in the php code for sending form submission by email. however its still causing me the same problem whereby when i go and submit the form, it redirects me to a page showing this part of my php code:
    Code:
     ' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $emailbody = "
    
    You have received a new message from the enquiries form on your website.
    
    Name: {$name}
    
    Email Address: {$email}
    
    Telephone: {$telephone}
    
    Enquiry: {$enquiry}
    
    Message: {$message}
    
    This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}
    "; mail("kid5er@yahoo.com"",""New Enquiry",$emailbody,$headers); } //what we need to return back to our form $returndata = array( 'posted_form_data' => array( 'name' => $name, 'email' => $email, 'telephone' => $telephone, 'enquiry' => $enquiry, 'message' => $message ), 'form_ok' => $formok, 'errors' => $errors ); //if this is not an ajax request if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){ //set session variables session_start(); $_SESSION['cf_returndata'] = $returndata; //redirect back to form header('location: ' . $_SERVER['HTTP_REFERER']); } }

    and this is my updated php code:
    Code:
    //send email if all is ok
        if($formok){
            $headers = 'From: <contact.php>' . "\r\n";
    		<script type="text/javascript">
    /* <![CDATA[ */
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
    /* ]]> */
    </script>
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
             
            $emailbody = "<p>You have received a new message from the enquiries form on your website.</p>
                          <p><strong>Name: </strong> {$name} </p>
                          <p><strong>Email Address: </strong> {$email} </p>
                          <p><strong>Telephone: </strong> {$telephone} </p>
                          <p><strong>Enquiry: </strong> {$enquiry} </p>
                          <p><strong>Message: </strong> {$message} </p>
                          <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";
             
            mail("kid5er@yahoo.com"",""New Enquiry",$emailbody,$headers); 
    		<script type="text/javascript">
    /* <![CDATA[ */
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
    /* ]]> */
    </script>
             
        }

  3. #3
    Join Date
    Feb 2014
    Posts
    23
    crap, I just realized need to have a web server and MySQL installed.. that would explain my issues!

  4. #4
    Join Date
    Feb 2014
    Posts
    23
    Okay, just installed microsoft webmatrix however I am still not recieving any emails when I do the form submission, the browser directs me to the process.php page where my php code is run from, when it should be alerting me whether the form submission went through or not.

  5. #5
    Join Date
    Oct 2013
    Posts
    514
    I don't know much about PHP so I'll leave analyzing that to someone else. However, you have a bunch of HTML errors some of which may or may not affect how your PHP is processing the input. Here's one example:

    Code:
    <input type="email" id="email" name="email" value="" placeholder="johndoe@example.com" script type="text/javascript" required ="required">
    /* <![CDATA[ */
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
    /* ]]> */
    Errors are highlighted.

    Use this instead:
    HTML Code:
    <input type="email" id="email" name="email" value="" placeholder="johndoe@example.com" required ="required">
    <script type="text/javascript">
    (function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
    </script>
    The script type="text/javascript" code in the input tag may have just been a bad copy/paste. However, it can't be there. First, <script> is it's own tag and can't be placed in another tag. Second, you have already declared the input as type="email". type="text/javascript" gets ignored at best, and replaces your desired type at worst.

    The CDATA opening and closing code is a leftover from, or unfortunate legacy of, XHTML. You have a HTML5 DOCTYPE (<!DOCTYPE html>) so this code is -- again -- at best useless, and at worst messing up something. I highly suggest deleting those lines from all your code.

    A couple more suggestions:
    1) Validate your HTML at http://validator.w3.org/. Some of your HTML errors would not affect your mailing script, but it's always good to start with clean code.
    2) You have a bunch of JavaScripts all over the place in your HTML. Use the JavaScript console in Chrome or Firefox to check them for errors. The one I highlight above has some funky stuff going on. First it's surrounded by parentheses [()] so it may not be usable. Second, the function doesn't seem to have a name, it's just "function ()".

    GIGO

    Good luck!

  6. #6
    Join Date
    Feb 2014
    Posts
    23
    Thanks for taking a look and commenting on this, yeah I noticed and fixed the issue with the script tag beginning inside of the input element, I fixed that up and then I just left the cdata because I read that it forces the xml parser to skip over the code in between its tags. I'm not experienced enough to know whether or not this is required in this situation, since I have other scripts running in the same code without those cdata tags. lol. i have more of those in my process.php page, ill try deleting them and running the code and see where it gets me. At this point I think that maybe I need to have a mysql database installed because otherwise where would the code that is being passed by the php be stored? I dunno ill keep trying

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    795
    Quote Originally Posted by 46andtool View Post
    At this point I think that maybe I need to have a mysql database installed because otherwise where would the code that is being passed by the php be stored? I dunno ill keep trying
    Whatever do you mean by this statement? Code is not stored in a db.

  8. #8
    Join Date
    Feb 2014
    Posts
    23
    sorry I didnt mean the code but the information that is populated in the form fields, where is that stored?

  9. #9
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    795
    Since programming is very much an exact science, one should also be sure that their writing is up to a similar level when conversing about it.

  10. #10
    Join Date
    Oct 2013
    Posts
    514
    Quote Originally Posted by ginerjm View Post
    Whatever do you mean by this statement? Code is not stored in a db.
    Oh gimme a break...
    You know and I know what he really meant. Storing the submitted data in a database. Let's not argue sematics here.

  11. #11
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    795
    Kevin - see my previous statement. And the word is 'semantics'.

  12. #12
    Join Date
    Oct 2013
    Posts
    514
    I posted before I saw that comment. And again, gimme a break for missing the 'n' on my keyboard....

  13. #13
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    795
    I hate when I miss a key too!

  14. #14
    Join Date
    Oct 2013
    Posts
    514
    I suppose my point is that this person is doing this, if you had read through his posted code, for the Salvation Army. Probably on a volunteer basis. I also do that for a Boy Scout Troop website. I tend to learn things only as I need them. My only advantage over the OP is time. I've been doing it for much longer. Don't know everything by any stretch, and am seriously lacking in PHP knowledge because I don't use it except for a couple of very small scripts.

    My apologies for arguing with you, ginerjm, and getting off-topic. It's just that a volunteer/non-pro should not be held to the same semantic, or other standards as someone who does web development for a living, or is a poser which the OP is definitely not.

    Your point about programming being an exact science is well taken though, and is kind of the point I made in my original response. Use the tools available to you to check your code/markup. Make sure you have every / and " and > in place because if they're not, you're screwed. Those are my usual mistakes ... well that and missing the 'n' on my keyboard.
    Last edited by Kevin2; 02-23-2014 at 02:54 PM. Reason: and missing the period key!

  15. #15
    Join Date
    Feb 2014
    Posts
    23
    Quote Originally Posted by Kevin2 View Post
    I suppose my point is that this person is doing this, if you had read through his posted code, for the Salvation Army. Probably on a volunteer basis. I also do that for a Boy Scout Troop website. I tend to learn things only as I need them. My only advantage over the OP is time. I've been doing it for much longer. Don't know everything by any stretch, and am seriously lacking in PHP knowledge because I don't use it except for a couple of very small scripts.

    My apologies for arguing with you, ginerjm, and getting off-topic. It's just that a volunteer/non-pro should not be held to the same semantic, or other standards as someone who does web development for a living, or is a poser which the OP is definitely not.

    Your point about programming being an exact science is well taken though, and is kind of the point I made in my original response. Use the tools available to you to check your code/markup. Make sure you have every / and " and > in place because if they're not, you're screwed. Those are my usual mistakes ... well that and missing the 'n' on my keyboard.
    thanks for backing me up, I volunteered to make the local Salvation Army a website because they sorely need one and cant find a web developer who has the time..I did it partially to help out and I also did it as a learning experience. I've always been curious about programming and web design in particular and figured diving right in would be good for me! As for that I am definitely very green and I find myself jumping onto google almost every five minutes to look up something etc. But its a good feeling when you finally have something working and you see your site taking shape in front of you.

    As for my original issue on this forum, I decided to create a test site specifically to trouble shoot my issues with the contact form, and see if isolating it from the rest of the code on my site wouldn't simplify pinpointing the issue with form submission. http://code.tutsplus.com/tutorials/b...orm--net-20426 <-- that is the tutorial that I am following for the contact form. As opposed to just copying and pasting every line of code I decided to try and work my way through all of it so that I would understand what I was doing, but somewhere along the way I made a mistake and now I am lost.

    For the test site I used the authors source files and this time, success! when I entered information into the contact forms and hit submit I got a message saying that the submission went through. Anyway I definitely made some mistake somewhere in my original code earlier, what makes it difficult is WebMatrix debugging tool isn't finding any problems so I am really confused as to what I did wrong. Looks like I am just going to copy and paste the authors code after all, as long as it works. I wish I knew what I was doing wrong and I will probably take some time to cross check the working code with the screwy code one more time just to see if I cant pinpoint my original error.

    I have one more question for you guys. I am running the site I am making on a local host to test it using WebMatrix, and since its a local host does that mean that I wont be able to receive the emails with the contact form data? I ask because although when I submit the form data and it tells me it went through successfully I am still not receiving any email with the contact form data.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles