-1

I have a submission form, and I wrote a PHP script to make form send an email with input data that the user inserts it in the fields of the form

Now I put the PHP code in the same file of HTML and CSS, I mean that the HTML, CSS and PHP in the same file

When I put the file in the CMS, that what happened to the page, you could see what happen from here: https://www.hochikiamerica.com/acd-landing-2

PHP Script and HTML code of the form https://github.com/Mstava/FreelancerProject/blob/master/formScript.php

Now, I need to know where to put the PHP code in the HTML file to avoid this

and How to ensure that code is working and it sends the Email

  • Which CMS is it, you cannot put PHP directly into wysiwig text editor of CMS.. Instead put your formScript.php on a web server and launch it from a form with a target=“_blank”, from CMS.. – estinamir Feb 11 '19 at 20:15
  • PHP is unlikely to be supported when the website runs on ASP. – mario Feb 11 '19 at 20:19
  • It's a proprietary, not Wordpress or other Known CMS, and all I have is access to the HTML editor – Mustafa Abdullah Feb 11 '19 at 20:22

2 Answers2

1

When you click on submit your form action takes you to action_page.php That's where you should read your post variables and send the email. Take a look at this post I wrote several years ago blue host email

DCR
  • 14,737
  • 12
  • 52
  • 115
  • I can't put PHP code in another page, it should be in the same file with HTML because of the CMS! – Mustafa Abdullah Feb 11 '19 at 20:25
  • @MustafaAbdullah - Yes, you can do it. Post another question asking how to do that with your CMS clearly specified. – cssyphus Feb 11 '19 at 20:27
  • If you want help you need to post what you've actually done. Show us your code and fully describe your environment – DCR Feb 11 '19 at 21:56
0

A common misconception is that additional PHP can be run upon the user doing something (i.e. clicking submit button). Not the case. When the page has been rendered, no further PHP on that page can be executed.

So, what to do?

You have two options:

(1) You can create a second page (action_page.php or some name), that is specified on the action= attribute of the form tag. That additional page will receive the data the user typed in, via PHP variables $_POST (if method="post") or via $_GET if method=get, and you can then use that data to send the contact form, and either display new data to the user or send the user back to the original page. Of course, you may need additional PHP to acknowledge the form has been sent, etc - and this additional code will need to handle both the case where the user is visiting the page for the first time, and when the contact form has been sent and the user is seeing the page for the second time.

(2) You can use AJAX (javascript/jQuery) to grab the form data, send it to a secondary PHP file, which will receive the data via the $_POST/$_GET variables, send the email, and return a response back to the first page.

These days, mostly we use the second method, because it is much more powerful. For one, the user remains on the same page. For another, there is no page refresh. For another, your javascript can do other things after the form has been sent.

AJAX is actually pretty simple - just do a google search for YouTube videos on creating a login system with PHP and AJAX. You should be able to find one of around 10 mins or less that explains all you need to know to send your contact form, and send feedback back to the calling page.

Here is a 5-minute YouTube tutorial that will show you the basics:

Install a simple PHP and Ajax login system

cssyphus
  • 37,875
  • 18
  • 96
  • 111