error with submitting a form in wordpress - php

i've created a plugin (simple form) and this is the shortcode.php file:
<?php
add_shortcode('contact_form','contact_form');
function contact_form(){
if (isset($_POST['submit'])){
global $wpdb, $table_prefix;
$name = $_POST['name'];
$data = array('name'=>$name,'message'=>'message');
$wpdb->insert($table_prefix.'contact_form',$data,array('%s','%s'));
echo 'added';
}
?>
<form method="POST" action="" id="contact_form">
Name: <input type="text" name="name"><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
}
when I submit the form, if I write sth in the text field, is says 'The page doesn't exist' but if i leave it empty, it submits the form.
what is the problem?
I have used this shortcode in a page.

See this question on the WordPress StackExchange site. In summary, WordPress gets confused by a field called name. Try renaming it to (say) contact_name.
Loads more detail here (also wpse).

Related

Creating a single file small PHP piece

Can anyone help me make this work? I am very very new to PHP and I was just trying to create a single file thing which asks for a name and returns it. Can anyone help see where I am going wrong? I am testing it on wtools.io
Thanks in advance for any help :)
<form method="POST" action="formFunction()" name="form1">
Name: <input type="text" name="name">
<input name="s1" value="Submit LoL !" type="submit">
</form>
<?php
function formFunction() {
$name = $_POST['name'];
echo $name;
}```
You cannot call a PHP function as the action of your form. Instead, you should create a second file/script, something like post.php, and use that as the action. For example:
<form action="post.php" method="post" name="form1">
Then, in your post.php:
<?php
if (!$_POST) {
print "This should only be called on form submit.";
exit();
}
// you should actually test to see if name is supplied before this next line
$name = $_POST['name'];
echo $name;
?>
<!-- do some other stuff... -->
A similar question for your review Calling a particular PHP function on form submit

Passing form values on PHP same page

I have a form in my page and also have different phps in the same page..how can i pass the form to one of the phps? Thank you
It goes like this.
<form action = 'samepage.php'
method='POST'>
<input>
<input type = 'submit'>
</form>
<?php
?>
<?php
?>
<?php
I want to pass the value here
?>
The form in HTML page is a structure that allow you to insert some value and then can be passed to the backend with a specific HTTP Method called as GET,POST,PUT, DELETE etc.
Here an example:
// HTML Page. index.html
<form action="page.php" method="POST">
<input type="text" name="username">
<input type="submit">
// page.php
<?php
$username = $_POST['username'];
echo "Your username is: " . $username;
?>

How to acess form details in php page using html 'id' attribute

i have a form having employeeNo,name and a submit button. when the form is submitted it redirects to a php page, Now the problem is i want to access the values of the form by using 'id' attribute?if possible i need the html and php code.
Is there a way to solve this Problem?
enter code here<html>
<head>
</head>
<body>
<form action="test.php" method="">
<input type="text" name='employeeNo' id="input1">
<br>
<input type="text" name='name' id="input2">
<br>
<input type="submit" id="input3">
</form>
</body></html>
In your test.php file you need to retrieve the values. Since you don't define any method in your form, by default it should be "GET"
Your php file could look like the following:
<?php
$employee_no = $_GET['employeeNo'];
$name = $_GET['name'];
I would suggest defining your form method as POST though, like the following:
...
<form action="test.php" method="POST">
...
so your php file will retrieve values from form like this:
<?php
$employee_no = $_POST['employeeNo'];
$name = $_POST['name'];
You may also look at this for further explanation:
https://www.w3schools.com/php/php_forms.asp

PHP form - on submit stay on same page

I have a PHP form that is located on file contact.html.
The form is processed from file processForm.php.
When a user fills out the form and clicks on submit,
processForm.php sends the email and direct the user to - processForm.php
with a message on that page "Success! Your message has been sent."
I do not know much about PHP, but I know that the action that is calling for this is:
// Die with a success message
die("<span class='success'>Success! Your message has been sent.</span>");
How can I keep the message inside the form div without redirecting to the
processForm.php page?
I can post the entire processForm.php if needed, but it is long.
In order to stay on the same page on submit you can leave action empty (action="") into the form tag, or leave it out altogether.
For the message, create a variable ($message = "Success! You entered: ".$input;") and then echo the variable at the place in the page where you want the message to appear with <?php echo $message; ?>.
Like this:
<?php
$message = "";
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
The best way to stay on the same page is to post to the same page:
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
There are two ways of doing it:
Submit the form to the same page: Handle the submitted form using PHP script. (This can be done by setting the form action to the current page URL.)
if(isset($_POST['submit'])) {
// Enter the code you want to execute after the form has been submitted
// Display Success or Failure message (if any)
} else {
// Display the Form and the Submit Button
}
Using AJAX Form Submission which is a little more difficult for a beginner than method #1.
You can use the # action in a form action:
<?php
if(isset($_POST['SubmitButton'])){ // Check if form was submitted
$input = $_POST['inputText']; // Get input text
$message = "Success! You entered: " . $input;
}
?>
<html>
<body>
<form action="#" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
Friend. Use this way, There will be no "Undefined variable message" and it will work fine.
<?php
if(isset($_POST['SubmitButton'])){
$price = $_POST["price"];
$qty = $_POST["qty"];
$message = $price*$qty;
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="#" method="post">
<input type="number" name="price"> <br>
<input type="number" name="qty"><br>
<input type="submit" name="SubmitButton">
</form>
<?php echo "The Answer is" .$message; ?>
</body>
</html>
You have to use code similar to this:
echo "<div id='divwithform'>";
if(isset($_POST['submit'])) // if form was submitted (if you came here with form data)
{
echo "Success";
}
else // if form was not submitted (if you came here without form data)
{
echo "<form> ... </form>";
}
echo "</div>";
Code with if like this is typical for many pages, however this is very simplified.
Normally, you have to validate some data in first "if" (check if form fields were not empty etc).
Please visit www.thenewboston.org or phpacademy.org. There are very good PHP video tutorials, including forms.
You can see the following example for the Form action on the same page
<form action="" method="post">
<table border="1px">
<tr><td>Name: <input type="text" name="user_name" ></td></tr>
<tr><td align="right"> <input type="submit" value="submit" name="btn">
</td></tr>
</table>
</form>
<?php
if(isset($_POST['btn'])){
$name=$_POST['user_name'];
echo 'Welcome '. $name;
}
?>
simple just ignore the action attribute and use !empty (not empty) in php.
<form method="post">
<input type="name" name="name">
<input type="submit">
</form>
<?PHP
if(!empty($_POST['name']))
{
echo $_POST['name'];
}
?>
Try this... worked for me
<form action="submit.php" method="post">
<input type="text" name="input">
<input type="submit">
</form>
------ submit.php ------
<?php header("Location: ../index.php"); ?>
I know this is an old question but since it came up as the top answer on Google, it is worth an update.
You do not need to use jQuery or JavaScript to stay on the same page after form submission.
All you need to do is get PHP to return just a status code of 204 (No Content).
That tells the page to stay where it is. Of course, you will probably then want some JavaScript to empty the selected filename.
What I do is I want the page to stay after submit when there are errors...So I want the page to be reloaded :
($_SERVER["PHP_SELF"])
While I include the sript from a seperate file e.g
include_once "test.php";
I also read somewhere that
if(isset($_POST['submit']))
Is a beginners old fasion way of posting a form, and
if ($_SERVER['REQUEST_METHOD'] == 'POST')
Should be used (Not my words, read it somewhere)

I want to convert a php/mysql application to a wordpress plugin

I have a php/mysql web application.I want to convert it in a wordpress plugin.How can I make link of one php file to another file.
For example I have plugin tw. tw.php is its index file and the other file name is tw1.php.
first page
/* plugin name:tw
plugin url:httpL://csr.estheticsolutions.com.pk
*/
//tell wordpress to register the demolistposts shortcode
add_shortcode("demo-list-posts", "demolistposts_handler");
function demolistposts_handler()
{
//run function that actually does the work of the plugin
$demolph_output = my_function();
//send back text to replace shortcode in post
return $demolph_output;
}
function my_function()
{
?>
<html>
<body>
<form action="tw1.php" method="post">
<input type="text" name="fname" >
<input type="submit" value="Submit" >
</form>
</body>
</html>
<?php
}
?>
second page
<?php
$First_Name=$_POST['fname'];
echo $First_Name;
?>
when i submit value in tw.php ,an error appears tw1.php not found(404) in wordpress folder,then i paste the file tw1.php in that(wordpress) folder and submit, posted value get in new page without wordpress theme.
Create a table with name=people with columns: id, firstname, lastname, DateofBirth, addressid, fatherid, motherid. Have suitable column properties with each column.
You can use the plugin url function to link to the second file:
<form action="<?php echo plugins_url( 'tw1.php', dirname(__FILE__) ); ?>" method="post">
<input type="text" name="fname" >
<input type="submit" value="Submit" >
</form>

Categories