0

I am trying to re-do my site to wordpress, I have this problem. I have a form and on a submit php file runs and adds data to database. How do I run php file in wordpress?

<form method="POST" action="insert.php"> 
<p>
    	<label for="XX">XX</label>
    	<input type="text" name="XX" id="XX">
      
      <label for="X">X</label>
    	<input type="text" name="X" id="X">
      <label for="XXY">XXY</label>
    	<input type="text" name="XXY" id="XXY">
      
</p>
    <input type="submit" value="add to database">
</form>					

How do I run the php insert.php file? Thank you!

Or on the other hand, could I write in action the directory to the file? eg. wp-content/myphpfiles/insertgolf.php, would that work? Thank you, I am new to wordpress

1
  • this form in temaplte or admin page content? Commented Feb 17, 2018 at 6:21

1 Answer 1

2

In WordPress form page, you can not put php file path. If you want to do this then you need to create a template.

Please follow reference link, here you can know how to create template in WordPress: https://www.cloudways.com/blog/creating-custom-page-template-in-wordpress/

In template file put your code:

<?php /* Template Name: insertGolf */ 
    if(!empty($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){

    //do database related code here

       $link = mysqli_connect("c108um.DDD.com","f96860","qt3C2EQ","f96860"); 
if($link === false){
   die("ERROR: " . mysqli_connect_error());
 } 
 $datum = mysqli_real_escape_string($link, $_REQUEST['datum']); 
 $odkud = mysqli_real_escape_string($link, $_REQUEST['odkud']); 
 $sql = "INSERT INTO golf (datum, odkud) VALUES ('$datum', '$odkud')";
 if(mysqli_query($link, $sql)){ header('Location: golf.php');
 exit;
 }
 else{ 
 echo "ERROR: " . mysqli_error($link);
 } 
 mysqli_close($link);
}
?>
<form method="POST" action=""> 
<p>
        <label for="XX">XX</label>
        <input type="text" name="XX" id="XX">

      <label for="X">X</label>
        <input type="text" name="X" id="X">
      <label for="XXY">XXY</label>
        <input type="text" name="XXY" id="XXY">      
</p>
    <input type="submit" value="add to database">
</form>
Sign up to request clarification or add additional context in comments.

10 Comments

when you create a template and then create a page, include that template on this page. Now left action="" blank. when you submit the form.
I hope it will make sense.
put your form code into the template and handle the request in that template.
you can use header('Location: nameofnewsite.com'); exit(); or
wp_redirect( $url ); exit;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.