Validate and continue php script after submit - php

I have a form, with some input elements (name, phone, address) like this:
<form method="get" action="<!-- PHP --> echo htmlspecialchars($_SERVER['PHP_SELF']); <!-- ENDPHP -->" accept-charset="utf-8">
<label>Nombre:</label>
<input name="nombre" id="nombre" type="text" placeholder="Tu nombre">
<label>Teléfono:</label>
<input name="phone" id="tel" type="text" placeholder="666777666">
<label>Dirección:</label>
<input name="address" id="direc" type="text" placeholder="Mi Casa">
<fieldset>
<input value="Enviar" type="submit">
</fieldset>
</form>
After that form, there is a large php code to "post" the obtained data in phpbb (using submit_post() ) and formatting the body and title post.
Then when click on submit I want to validate form data (verifying that the phone is a number, for example) and, if all is ok, continue with the php script to post.
I have searched a bit, and found that is needed action="<!-- PHP --> echo htmlspecialchars($_SERVER['PHP_SELF']); <!-- ENDPHP -->" but when click on submit, page only reloads, without running the php code just below the form.
PD: this will be a html file to be used as template for phpbb3 so instead of <?php ... ?> is needed to use <!-- PHP --> .... <!-- ENDPHP -->
Any help or code is appreciated

Try this,
You need to enable PHP in templates in Security section of Admin Panel.
Make sure you refresh the template from Admin Panel and clear your browser cache after you've modified the code

Related

Trying to use PHP in HTML?

I'm using Wamp server to code a website on my PC, I'm following a tutorial on W3Schools for <form> and data collection with PHP (the GET method) and even though I followed the tutorial the code doesn't work.
I researched it a little here but no one seems to be able to answer my problem.
<form action="welcome_get.php">
<section class="contact bg-primary" id="contact">
<div class="container">
<h2>Abonnez vous par mail!</h2>
<input type="email" id="email" size="50" placeholder="Entrez votre adresse e-mail"></input>
</div>
<div class="mail-submit">
<input type="submit" id="submit" value="Je m'abonne"></input>
</div>
</section>
</form>
That is the HTML before I send the email address, and this is the simple page to GET the email:
<html>
<body>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
Yet it doesn't work at all. Here is the tutorial i followed
https://www.w3schools.com/php/php_forms.asp
Update
I made a mistake. I was opening the FILE through my browser NOT accessing through Wampserver.
Now the issue is when I click on Submit button (that calls the
<form action="welcome_get.php" method="get">) it downloads the welcome_get.php.
Your form is not set method POST or GET :
<form action="welcome_get.php" method="get">
First :
add method GET
<form action="welcome_get.php" method="get">
Second :
add attributte name in input
<input type="email" name="email" id="email" size="50" placeholder="Entrez votre adresse e-mail"></input>
Final :
make sure your extension is .php not .html
I think your problem is related to your cache browser, what I advise you to use
another browser to test your PHP script or try to clear your cache browser and then it should work.

Multiple forms using the action variable?

I have a page that has multiple php forms that send me different informations depending on the user and the form. How do I make php differentiat these? I saw something about the actions and I thought maybe it was like having a separate file, but when I created a "questions.php" and copied and pasted my php code into that and then added "action="questions.php"" to the tag, and ran it just as I had before it didn't work. So what is the correct way to do this?
My code is extremely long and filled with words which is why it would be nice to have separate files for it depending on the form instead of all in the top of my main page.
You could build one page as a standard page. in this page you could include the different forms. See the example below:
<!-- These are just example names -->
<div class="form-1">
<?php include "forms/form-1.php"; ?>
</div>
<div class="form-2">
<?php include "forms/form-2.php"; ?>
</div>
<div class="form-1">
<?php include "forms/form-3.php"; ?>
</div>
If you would like to save data that a user puts in a form in lets say a database you should use the <form method="post"> by adding an action to it. You're asking the form to send the user to another page when the submit button is clicked. an example of a form down below.
<form class="form-1" method="post" action="second_page.php">
<label>Name:
<input type="text" class="input" name="name">
</label>
<label>Email:
<input type="email" class="input" name="email">
</label>
<button type="submit">Send!</button>
</form>
Hope this helps!
I just added a / before the questions.php. apparently it had to do with file path? I don't understand it but it works now.
<form action="questions.php" method="post">
<!-- text boxes to get user inputs-->
<button type="submit" value="submit_btn">click here to go to page in action tag
</button>
</form>
If questions.php file is in same folder action="questions.php" is enough. If its inside 'x' folder, correct path should be given as action="x/questions.php"

HTML form to GET two variables from a PHP page

I've been working for several hours trying to get this to work properly. The page I have a form on is /index.php?action=pagename. I have a form that needs to get a variable from the following /index.php?action=pagename&thing=something. My HTML form going like this:
<form role="form" action="pagename" method="get">
<input type="text" name="thing">
</form>
This form is located on /index.php?action=pagename and I want to get &thing from that URL.
Any ideas?
The problem I'm having is that when the form is submitted, the URL redirects to index.php?thing instead of staying on index.php?action=pagename.
It looks like you might be having some trouble with <forms> in general and not just PHP so here is an overview:
<!-- the action is where you want to send the form data -->
<!-- assuming THIS code is the index.php file then we want to send the data to ourselves -->
<!-- the method is GET so it will be directly accessible from the URL later -->
<form action="index.php" method="GET">
<!-- add a hidden value for pagename -->
<input type="hidden" name="action" value="pagename">
<!-- the name called "thing" will be appended to the URL and it's value as well -->
<input type="text" name="thing" value="<?php echo (isset($_GET['thing']) ? $_GET['thing'] : ''); ?>">
<br>
<!-- click this button to submit the form to itself -->
<!-- once this has been submitted then you can retrieve the URL value with $_GET as you can see above -->
<input type="submit" value="submit" />
</form>
You can simply use the PHP-variable $_GET['thing'] to get the value.
The action attribute of the form element is the target script which will be called on submit. If blank it will be the current script which is showing the form. You also can only give url parameters beginning with ?.
<form role="form" action="?action=pagename" method="get">
<input type="text" name="thing">
</form>
Exerpt from php.net: http://www.php.net/manual/de/tutorial.forms.php
One of the most powerful features of PHP is the way it handles HTML
forms. The basic concept that is important to understand is that any
form element will automatically be available to your PHP scripts.
Please read the manual section on Variables from external sources for
more information and examples on using forms with PHP. Here is an
example HTML form:
Example #1 A simple HTML form
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
There is nothing special about this form. It is a straight HTML form
with no special tags of any kind. When the user fills in this form and
hits the submit button, the action.php page is called. In this file
you would write something like this:
Example #2 Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
A sample output of this script may be:
Hi Joe. You are 22 years old.
Basic script I made to get the variables from the URL:
<?php
if (isset($_GET["action"]) && isset($_GET["thing"])) {
$action = $_GET["action"];
$thing = $_GET["thing"];
echo $action .PHP_EOL . $thing;
}
?>
This will output the following for the URL /index.php?action=pagename&thing=something
pagename
something
Though you should probably learn how to use forms properly first.

Wordpress Recaptcha use on form that created using page

I am trying to use a wordpress recaptcha plugin.
The plugin (like wp-recaptcah, better Wp recaptcha) that I am trying to use is only working for the comment form.
But in my case I have created a page and there I have inserted HTML that is managed from the admin panel.
I don't understand how I can use the plugin with my form.
I did not get any shortcode HTML tag to use it directly on the page.
Any suggestion how I can achieve this?
I have created a page in the admin panel and inserted this code. My form is submitted via AJAX:
<div class="First"> Here some static Html is used </div>
<form id="main-form">
<div>
<label>Name</label>
<input class="span6" type="text" maxlength="45" name="name" />
</div>
<div>
<label>Email</label>
<input class="span6" type="text" maxlength="45" name="email" />
</div>
<div>
<label>Phone Number</label>
<input class="span6" type="text" name="phone" placeholder="Enter Phone Number" />
</div>
<div>
<label>Message</label>
<textarea class="span12" name="message" rows="6" ></textarea>
</div>
<div><button class="btn >Send Enquiry</button></div>
</form>
<div class="second">Here some static Html is used </div>
Best suggestion: use Contact Form 7 and Contact Form 7 reCAPTCHA Extension or the Really Simple Captcha plugin, both work well with Contact Form 7.
You'll create the form on the CF7 admin page, then insert it into your WordPress page with a shortcode, which, for many reasons, is much better than writing the form itself in the WordPress text view.
You may use reCAPTCHA Form . It give u a form along with the recaptcha.
http://wordpress.org/plugins/recaptcha-form/
or else you may be wanting the following one instead,
http://wordpress.org/plugins/wp-recaptcha/

Avoiding duplicate code between php and javascript

I have a php page with a form on it for adding people to a small group.
For each person being added, there is a with multiple form elements, each named according to the person's number. For example:
<div class="user">
<input type="text" name="user1LastName" />
...
</div>
<div class="user">
<input type="text" name="user2LastName" />
...
</div>
For each person in the database, the php page populates a form sections.
To add additional people, the user can click on a "+" icon, at which time the page uses jQuery to dynamically populate a new . To do this I am simply appending the new div html to the existing form. This means that the javascript page contains all the same html markup (to be appended), as the php page.
This seems like an unnecessary duplication of code. Whenever I change something in the php page, I also have to change it in the javascript code.
Is there any general method for avoiding such code duplication? The only thing I can think of is to use jQuery to grab the html from an already existing div. But in this case, the values of the form fields for user n will appear in the new code for user n+1.
Thanks.
Capisci :)?
<div class="user" id="user_1">
<input type="hidden" name="uid[0]" value="1"/>
<input type="text" name="lastname[0]" value="user480029"/>
...
</div>
<div class="user" id="user_2">
<input type="hidden" name="uid[1]" value="2"/>
<input type="text" name="lastname[1]" value="arto"/>
...
</div>
Now when adding another field just...
<div class="user" id="user_3943094103945">
<input type="hidden" name="uid[]" value=""/>
<input type="text" name="lastname[]" value=""/>
...
</div>
Then you iterate trough $_POST[] a do what you want.
You have user ID on .user, so I you delete user you can remove that part of HTML (this is more for UX), more importantly, you don't have hundreds of variables but just a few array which you can iterate in one loop. Hope you get the point. Cheers.
The php code should give the javascript a "prototype", which could be modified using javascript. That way, even if there aren't any users, the javascript would still work. This example is obviously missing lots of code (like forms), but it should give you an idea. I haven't tested it because I assume you have to make lots of modification anyways.
<script type="application/x-javascript">
addEventListener("load",function(){
document.getElementById("add-user").addEventListener("click",function(){
var node=document.getElementById("prototype-container").getElementsByClassName("users")[0].cloneNode(true),n=document.getElementById("add-user").getElementsByClassName("users").length,list=node.getElementsByTagName("input");
document.getElementById("user-list").appendChild(node);
node.id="users_"+(n+1);
for(var i=0;i<list.length;++i)
list[i].name&&(list[i].name+="["+n+"]");
},false);
},false);
</script>
</head>
<body>
<div id="prototype-container">
<? /* print out a div without any information in it */ ?>
</div>
<div id="user-list">
<? /* print out divs with some infomation in them */ ?>
</div>
<button id="add-user">add a user</button>

Categories