Jump to content

Contact form does not work on dynamic site (blank data)


rsdias
Go to solution Solved by rsdias,

Recommended Posts

Good afternoon, I'm new here, I'm starting in php, html, css, I'm making my website, and I came across a first problem, I'm making it dynamic but without using a database for now, but I load the pages through:

<?php

function escolhe_pagina() {
  (isset($_GET['p'])) ? $pagina = $_GET['p'] : $pagina = 'home';
  if(file_exists($pagina.'.php')):
    require_once($pagina.'.php');
  else:
    require_once('home.php');
  endif;
}

 ?>

 

My index.php looked like this:

<?php

require_once('funcao_escolhe_pagina.php');   // Function to Select the Clicked Page Dynamically
require_once('funcao_escolhe_titulo.php');   // Function to Select the Title according to the Accessed Page
require_once('funcao_escolhe_estilo.php');   // Function to Select the Style according to the Accessed Page
//
require_once('header.php');   // HTML data
require_once('navbar.php');   // Menu Data
escolhe_pagina();             //require_once('home.php');     // Body Data - Home or Other Pages
require_once('footer.php');   // Page footer data

 ?>

 

That is, it has the standard header, navbar and footer pages fixed using require_once, and chooses the page that can be home, contact, newsletter, etc.

It is calling the pages correctly, but when I enter the contact or newsletter page it enters, but after filling out the form and it does not call the envio_email_contato.php page in the action and if I put a link instead of input button there it sends the e-mail but the contact data is sent blank, without filling, I don't know how to solve it if anyone can help me, thank you in advance.

Roberto S. Dias

 

Link to comment
Share on other sites

1 hour ago, rsdias said:

My index.php looked like this:

<?php

require_once('funcao_escolhe_pagina.php');   // Function to Select the Clicked Page Dynamically
require_once('funcao_escolhe_titulo.php');   // Function to Select the Title according to the Accessed Page
require_once('funcao_escolhe_estilo.php');   // Function to Select the Style according to the Accessed Page
//
require_once('header.php');   // HTML data
require_once('navbar.php');   // Menu Data
escolhe_pagina();             //require_once('home.php');     // Body Data - Home or Other Pages
require_once('footer.php');   // Page footer data

 ?>

 

That is, it has the standard header, navbar and footer pages fixed using require_once, and chooses the page that can be home, contact, newsletter, etc.

It is calling the pages correctly, but when I enter the contact or newsletter page it enters, but after filling out the form and it does not call the envio_email_contato.php page in the action and if I put a link instead of input button there it sends the e-mail but the contact data is sent blank, without filling, I don't know how to solve it if anyone can help me, thank you in advance.

Roberto S. Dias

 

It's looks like you are trying to do too many things at the same time. What I can guess is your are trying to do a contact page that sends email, pagination and accessing a database table? Though that is only a guess. My suggestion would to take you one thing at a time and start of basic. Learn the fundamentals of PHP and I know it can be boring, but it gives you a solid foundation on coding in PHP. I have been using PHP for a long time now and I still learn new things from online tutorials and from people who know more than me about PHP. There are plenty of tutorials just make sure that it's a current tutorial. Like already stated if you know the problem then show us that section of the code or HTML markup with the code. Though just looking at what you shown us, a configuration file at the top of the page would help you out for a lot of things and prevent you from typing in repetitive code.

Edited by Strider64
Link to comment
Share on other sites

The problem is that when I enter the newsletter or contact page and fill in the data and click on the send or register button, the page returns to home and does not send any information by email, but I have this same site, without being dynamic and it works correctly , so I don't know why this way it doesn't enter the sending of the e-mail, that's what I'd like to understand.

Link to comment
Share on other sites

slightly off topic, but don't do the following -

2 hours ago, rsdias said:

function escolhe_pagina() {
  (isset($_GET['p'])) ? $pagina = $_GET['p'] : $pagina = 'home';
  if(file_exists($pagina.'.php')):
    require_once($pagina.'.php');
  else:
    require_once('home.php');
  endif;
}

$_GET['p'] can be anything and cannot be trusted. by submitting a value that uses directory traversal, any .php file anywhere on your server can get required, allowing for example, an administrative action page to be accessed, since the resulting path/file will exist and that code will happily require and execute it for the current visitor.

you must validate all inputs before using them. you must test in your code that the $pagina value is only and exactly a permitted, directly accessible page.

back to the topic, we cannot help you with what's wrong with your code unless you post all the code need to reproduce the problem. there are multiple different ways to accomplish a task, and without knowing exactly what your code is, no one can simply tell you what to do to make your code work.

Edited by mac_gyver
Link to comment
Share on other sites

Hello, good morning, follow all the files, if someone can simulate and tell me what is happening that does not send the email, I would appreciate it.

I just deleted the email and password in the files envio_email_newsletter.php and envio_email_contato.php in the fields
   $mail->Username = '';
   $mail->Password = '';
   $mail->setFrom('', 'Attendance');
   $mail->addAddress('', 'Robert');

I'm not able to post the files here, how can I send the whole project ???

 

 

Link to comment
Share on other sites

We don't really need your entire project, but the answer would be to put it up in a github repo, and provide people a link to that.  If there are any credentials in config files, make sure you remove those, if you go that route.

With that said, it appears that the problem is concentrated in the email sending portion of things.  Getting emails to be sent properly is a non-trivial system administration exercise.  

Post the class and implementation code that actually sends the email, so we have a better idea of what that is.

  • Are you using  an smtp class?
  • Do you have a local/workstation version of the app, and have you tested it locally using  a mail trap like mailhog?
    • This is the modern way to work on an app and allows you to ascertain whether your issues are code based, or mail configuration based

This is an incredibly common issue, and 98% of the time, the code is correct, but there is some hosting issue or relaying issue preventing email from being sent or handed off.  

Link to comment
Share on other sites

gizmola, the e-mail sends correctly yes, I have the same file sent but the site is static, when I changed it to the dynamic form that I couldn't do it anymore, but the error is that send I press the send button (submit) it looks like the file that should be called to send the e-mail of this is not called for some reason in a dynamic way that I still haven't figured out why, I'll see if I create a github and post all the files there to see if anyone can help me. Thanks.

Link to comment
Share on other sites

On 4/11/2022 at 5:49 AM, rsdias said:

after filling out the form and it does not call the envio_email_contato.php page in the action

 

On 4/11/2022 at 8:11 AM, rsdias said:

the page returns to home

the only thing we can deduce based on the above two items is that there's something wrong somewhere, starting with the form and ending with however the code (finally) reaches the 'home' page. to directly solve this, we need see the actual (not a picture) of all the code needed to reproduce the problem.

On 4/11/2022 at 5:49 AM, rsdias said:

if I put a link instead of input button there it sends the e-mail but the contact data is sent blank

this indicates that the form processing code isn't first detecting if a post method form was submitted before doing anything at all, since a link would cause a get request for the page. this also indicates that the form processing code doesn't have any/enough server-side validation logic. the email should never get sent if there's no input data.

Link to comment
Share on other sites

Do you really expect us to read thru ALL THAT CODE?

In my first post I asked to see the form that was not working for you.   You never showed it.  How about - since you say the form is a problem - you show us this form and also the code block that is supposed to process it and we can start there?  

Link to comment
Share on other sites

Hi, Ginerjim, I already did this in the post, I explained that after I changed the site from static to dynamic the newsletter and contact form, even when I press the send button, it does not send the email, these two forms when pressed the button should call envio_email_contato. php and envio_email_newsletter.php, I'm starting programming and I have many doubts but the best way to learn is by making mistakes and correcting, right, if anyone can shed some light on this problem, I'd appreciate it. Thanks.

Link to comment
Share on other sites

edit: it turns out the main problem isn't in the form or form processing code -

the reason for the incorrect operation of the form(s) is because the markup on the page is broken. there's an opening <form .. tag on line 33 in navbar.php, for a search feature, that is never closed. since nested forms are invalid, any <form tag after that point on the page is ignored. i determined this by noticing the browser address bar changing to include the form field values, meaning a get method form was in effect. since the intended form is a post method form, i searched through the code to find all form tags, leading to the one on line 33 of navbar.php, with no corresponding closing </form> tag.

you need to validate all your resulting web pages at validator.w3.org

some items for your form processing code -

  1. the form processing code and the form should be on the same page. this results in the simplest code and the best User eXperience (UX.)
  2. the form processing code should first detect if a post method form has been submitted, trim, then validate all the input data, storing user/validation errors in an array using the field name as the array index, then only use the data if there are no validation errors. if there are validation errors, you would display them when you re-display the form and re-populate the form field values with the existing values so that the user doesn't need to keep reentering data over and over. they can just correct the validation errors.
  3. the only redirect in your code should be to the exact same url of the current page to cause a get request for that page. this will prevent the browser from trying to resubmit the form data if the user reloads the page or browses away from and back to that page.
  4. if you want to display a one time success message, dialog box, alert,... store the message in a session variable, then test, display, and clear that session variable at the appropriate location in the html document.
Edited by mac_gyver
  • Great Answer 1
Link to comment
Share on other sites

mac_gyver, thanks for the answer, I didn't understand very well, but I will try to test this problem over the weekend, I apologize if I ask something silly, I'm starting in this type of programming and alone without courses, if you can be patient with a beginner I appreciate it. As soon as I test and try to identify the problem, I'll post it here. Thank you all.

ginerjm, I'm sorry for the simplicity, I'm just starting so I may ask questions that seem silly to most, but for me it's very difficult to understand, thanks for the tips too.

 

Link to comment
Share on other sites

  • Solution

I couldn't wait for the weekend, I got a little time and I already did the test here correcting as mac_gyver had given me and everything went well.

mac_gyver, thank you very much, really it was an error in the navbar not closing the <form> tag.

Thank you to everyone who also collaborated and took the time to help me. Thank you so. Sorted out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.