connect form page on wordpress with php file - php

I created form on one page in my wordpress:
<form action="scores_send.php" method="post">
<strong>Date: </strong><input type="date" name="date" /><br />
<strong>TEAM 1:</strong>
Player 1.: <input type="text" name="team1_player1" /><br />
Player 2.: <input type="text" name="team1_player2" /><br />
<strong>TEAM 2:</strong>
Player 1.: <input type="text" name="team2_player1" /><br />
Player 2.: <input type="text" name="team2_player2" /><br />
<strong>SCORE:</strong>
Team 1. <input type="number" name="score_team1" min="0" max="5"/> : <input type="number" name="score_team2" min="0" max="5"/> Team 2.<br />
<input type="submit" value="Add" />
</form>
And also I wrote php code which connect to my external (no-wordpress) postgres database (connection parameters in code are not true) and insert information from form to one table:
<?php
$date = $_POST['date'];
$t1p1 = $_POST['team1_player1'];
$t1p2 = $_POST['team1_player2'];
$t2p1 = $_POST['team2_player1'];
$t2p2 = $_POST['team2_player2'];
$score1 = $_POST['score_team1'];
$score2 = $_POST['score_team2'];
if($date and $t1p1 and $t1p2 and $t2p1 and $t2p2 and $score1 and $score2) {
$connection = pg_connect("host=127.0.0.1 port=5432 dbname=scores user=user password=pass")
or die('Brak polaczenia z serwerem PostgreSQL');
$db = #pg_select_db('scores', $connection)
or die('Nie moge polaczyc sie z baza danych');
$team1 = #pg_query("INSERT INTO scores.games (score_team1, score_team2, datetime, team1_player1, team1_player2, team2_player1, team2_player2) VALUES ('$score1', '$score2', '$date', '$t1p1', '$t1p2', '$t2p1', '$t2p2'");
if($team1) echo "Scores added.";
else echo "ERROR! Scores not added";
pg_close($connection);
}
?>
I tried 2 ways:
1. to create template php file in folder of my theme and
2. to paste this code to content editor in wordpress (using this plugin: https://wordpress.org/plugins/insert-php/)
and both ways is not working. I changed part "action" in , but maybe wrong.

Just change your action of your form to : http://yoursite.com/scores_send.php
make sure that your scores_send.php is on the root of your installation.
So, your form look like :
<form action="http://yoursite.com/scores_send.php" method="post">

You should use http://example.com/page-slug/ in form action
Where page-slug is the slug of another page create by make scores_send.php as template.It will post all form value to score_send.php.From where you can proceed further to insert into external DB.
<form action="http://example.com/page-slug/" method="post">

Related

Problem: Part of the PHP exercise does not run correctly

I created a section for editing. When I edit the information and click the save button, the information is not saved and the header section does not display completely.
<?php
if (isset($_POST['submit_btn']))
{
$id = $_POST['id'];
$fn = trim($_POST['name']);
$ln = trim($_POST['lastname']);
$age = trim($_POST['age']);
$q = "UPDATE `users` SET `fn` = '$fn',
`ln` = '$ln',
`age` = '$age'
WHERE id = '$id'";
mysqli_query($dbconnect,$q);
if (mysqli_affected_rows($dbconnect) > 0)
redirect("?msg=ok&id=**$id**");
else
redirect("?msg=error&id=**$id**");
}
else
echo ("Not In If(isset)");
?>
<form action="" method="post">
<label for="name">FirstName:</label>
<input type="text" name="name" id="name" value="<?php echo $row['fn']?>">
<br>
<label for="lastname">LastName:</label>
<input type="text" name="lastname" id="lastname" value="<?php echo $row['ln']?>">
<br>
<label for="age">Age:</label>
<input type="text" name="age" id="age" value="<?php echo $row['age']?>">
<br>
<input type="submit" name="submit_btn" value="Save">
<a href="index2.php">
Back
</a>
</form>
</body>
Bold sections do not work here.
Below is a picture of the result:
In the link that I specified, after clicking on save the ID will not be displayed and all the information filled in the forms will be lost.
Sorry if the result is styleless and boring and I just created this page to practice php😁
Thank you for being responsive🙏🙏🙏
You are mistaking a POST request with a GET request.
Part, which appears in the URL is sent to the webserver in GET request.
Your form is submitting POST request to the webserver, logic in the code does the same, but you are trying to display information from url (GET).
Please check the examples in php.net:
POST variables: https://www.php.net/manual/en/reserved.variables.post.php
GET variables: https://www.php.net/manual/en/reserved.variables.get.php
You can take an example with GET request variable below, however, be careful with trusting the "end client" and always prepare your statements, which you send to your database to execute queries.
if (isset($_GET['submit']))
{
$number = $_GET['number'];
echo $number
? "Number which was submitted: $number <br>"
: 'Number is not set';
} else {
echo 'Form has not been yet submitted';
}
?>
<form action="" method="get">
<input type="number" name="number" placeholder="Number">
<input type="submit" name="submit" value="Save">
</form>

wordpress run php file in form action

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
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>

PHP simple form not posting

I've been tearing my hair out trying to figure out why the isset($_POST['Submit']) is not executing with my form. The data from the form is just not passing into the php code. Basically the code does not seem to be recognizing something like $ffname = $_POST["ffname"];
<?php
$ffname = $flname = $femail = $fcemail = $fpass = $fcpass = "";
if(isset($_POST['ffname'])){
$ffname = $_POST["ffname"];
$flname = $_POST["flname"];
$femail = $_POST["femail"];
$fcemail = $_POST["fcemail"];
$fpass = $_POST["fpass"];
$fcpass = $_POST["fcpass"];
echo "<p>Hello World<p>";
$con = mysqli_connect("localhost", "root", "") or die(mysqli_error());
mysqli_select_db($con, "userdata") or die(mysqli_error($con));
mysqli_query($con,"INSERT INTO tbluser (fname, lname, email, pass) VALUES('$ffname', '$flname', '$femail', '$fpass')")
or die (mysqli_error($con));
}
?>
<form method="post">
First Name: <input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Last Name: <input type="text" name="flname" value="<?php echo $flname;?>"><br>
E-mail: <input type="email" name="femail" value="<?php echo $femail;?>"><br>
Confirm E-mail: <input type="email" name="fcemail" value="<?php echo $fcemail;?>"><br>
Password: <input type="password" name="fpass" value="<?php echo $fpass;?>"><br>
Confirm Password: <input type="password" name="fcpass" value="<?php echo $fcpass;?>"><br>
<input type="submit" name="Submit" value="submit">
</form>
The other answer by #DerVO is correct. But there seems to be something else at play, since you say it still doesn't work.
A comment became too long, so I've built a full answer here.
Step 1:
Add a name to your input:
<input type="submit" name="Submit" value="submit">
However, relying on the submit in your $_POST is not the best plan. So I suggest watching a different form field - for example, ffname:
Step 2:
Improve your watch, using a different field:
if ( isset( $_POST['ffname'] ) ) {
// do your work
}
Lastly, you may be munging your form action attribute.
Step 3:
In order to keep things simple, if the form is supposed to submit to the same page, you can simply omit the form action.
<form method="post">
Betweeen these three items, the form will work, unless you have some problem with your server.
Step 4:
Clean up your form formatting. You've got odd spacing which is problematic. In an html element, the property="value" code needs to be without spaces, but spaces between properties. Example:
<!-- Your version -->
<input type = "text"name = "ffname"id = "ffname"value="<?php echo $ffname;?>"><br>
<!-- Clean / correct version -->
<input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Here's a "clean" version of your whole form:
<form method="post">
First Name: <input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Last Name: <input type="text" name="flname" value="<?php echo $flname;?>"><br>
E-mail: <input type="email" name="femail" value="<?php echo $femail;?>"><br>
Confirm E-mail: <input type="email" name="fcemail" value="<?php echo $fcemail;?>"><br>
Password: <input type="password" name="fpass" value="<?php echo $fpass;?>"><br>
Confirm Password: <input type="password" name="fcpass" value="<?php echo $fcpass;?>"><br>
<input type="submit" name="Submit" value="submit">
</form>
You need to give your input submit a name:
<input type="submit" name="Submit" value="Submit">
You have pass name of element in $_POST
try put name attribute in input submit
<input type = "submit" name="Submit" value = "1">

unable to post data from php form into mysql

No matter how many sites I go to to try and get this to work, it still doesn't. I am unable to have the data that has been entered end up in the database. The form submits, but doesn't come back with an error. When looking in phpMyAdmin, there are no records. I've tweaked it a million times with no luck. Can a second set of eyes show me what I'm doing wrong?
Thanks!!
Tim
<body>
<form action="insert.php" method="post"><br>
Date: <input type="text" name="date" id="date"><br>
Time: <input type="text" name="time" id="time"><br>
City: <input type="text" name="city" id="city"><br>
Fire Dept: <input type="text" name="fire" id="fire"><br>
Address: input type="text" name="addy" id="addy"><br>
Call Type/Level <input type="text" name="level" id="level"><br>
Description: <input type="text" name="desc" id="desc"><br>
Units: <input type="text" name="units" id="units"><br>
Submitted by: <input type="text" name"who" id="who"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])){
$db=mysql_connect("", "", "");
$mydb=mysql_select_db("rm911_incidents");
$date=$_Post['date'];
$time=$_Post['time'];
$city=$_Post['city'];
$fire=$_Post['fire'];
$addy=$_Post['addy'];
$level=$_Post['level'];
$desc=$_Post['desc'];
$units=$_Post['units'];
$who=$_Post['who'];
$sql = "INSERT INTO incidents(date,time,city,fire,addy,level,desc,units,who)
VALUES
('$date','$time','$city','$fire','$addy','$level','$desc','$units','$who')";
$result = mysql_query($sql);
if($result)
{
echo "<br>Input data is successful";
}
else
{
echo ("<br>Input data has failed");
}
}
?>
Okay, so I have fixed the error that Leo mentioned (Thank you!), however that was not the problem either. The errors that I am getting using the error reporting that you provided are: Undefined index: xxx... (xxx being every field name in the db). I have an 'id' field in the db, auto_increment-ing - did I forget to set that as the index?
The POST data is stored in the $_POST array, not the $_Post array. You should get a Notice on the undefined variable in your server log or in the browser (if PHP messages are sent to the browser).
The problem is in your post array name $_Post
$date=$_Post['date'];
$time=$_Post['time'];
$city=$_Post['city'];
$fire=$_Post['fire'];
$addy=$_Post['addy'];
$level=$_Post['level'];
$desc=$_Post['desc'];
$units=$_Post['units'];
$who=$_Post['who'];
Instead of $_Post you should use $_POST. Also, you can do
var_dump($_POST);
at the top of php file, so you will be able to see what the form sends to your script.
Regards
changed a few things, this should work.. if it doesnt is most likely an issue with your db
<html>
<body>
<form action="insert.php" method="POST"><br> <!-- method POST-->
Date: <input type="text" name="date" id="date"><br>
Time: <input type="text" name="time" id="time"><br>
City: <input type="text" name="city" id="city"><br>
Fire Dept: <input type="text" name="fire" id="fire"><br>
Address: <input type="text" name="addy" id="addy"><br>
Call Type/Level <input type="text" name="level" id="level"><br>
Description: <input type="text" name="desc" id="desc"><br>
Units: <input type="text" name="units" id="units"><br>
Submitted by: <input type="text" name="who" id="who"><br> <!-- name"who" changed -->
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
$db=mysql_connect("", "", "") or die ("Cant connect");
$mydb=mysql_select_db("rm911_incidents") or die ("Cant find db");
$date = $_POST['date']; //_POST not _Post
$time = $_POST['time'];
$city = $_POST['city'];
$fire = $_POST['fire'];
$addy = $_POST['addy'];
$level = $_POST['level'];
$desc = $_POST['desc'];
$units = $_POST['units'];
$who = $_POST['who'];
$sql = "INSERT INTO incidents ( date , time, city , fire , addy , level , desc , units , who )
VALUES ( '$date' , '$time' , '$city' , '$fire' , '$addy' , '$level' , '$desc' , '$units' , '$who' )";
$result = mysql_query($sql) or die ("Cant insert");
if($result)
{
echo "<br>Input data is successful";
}
else
{
echo "<br>Input data has failed";
}
}
?>
Also you're missing an opening square bracket:
Address: input type="text" name="addy" id="addy"><br>
Should be:
Address: <input type="text" name="addy" id="addy"><br>
Try doing this in your php file at the very top after the <?php:
//error reporting set to all
error_reporting(E_ALL);
ini_set('display_errors', 'On');
Also place your php code before your html.
You should see errors being outputted and be able to debug it.
Post the errors here if you need help.
can you check if Submitted by: <input type="text" name"who" id="who"><br> is causing error..
it should be Submitted by: <input type="text" name="who" id="who"><br>
If this doesnt help.. Try running a static insert query in your php file and check if its working...
Regards,
Leo

HTML submit forms using PHP

I am learning PHP now, so pardon my silly question which I am not able to resolve.
I have created a simple web form where in I display the values entered by a user.
function submitform()
{
document.forms["myForm"].submit();
} // in head of html
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" onclick="submitForm()" />
</form>
and res.php contains:
foreach($_POST as $field => $value)
{
echo "$field = $value";
}
When I click on the submit button, I just get a blank page without any values from the form. Can anyone please let me know what am I missing?
There's no need for the javascript. This should do:
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" />
</form>
Let's start with fixing errors:
JavaScript is case-sensitive. I see that your function name is submitform and the form's onclick calls submitForm.
The javascript is not really necessary from what you've shown us, I would try this on a single php page and see if it works:
Create a test.php file for test purpose:
<?php
if($_POST){
foreach($_POST as $key=>$value){
echo "$key: $value<br />";
}
}else{
<form action="test.php" method="post">
<input type="text" value="1" name="name1" />
<input type="text" value="2" name="name2" />
<input type="submit" value="submit" name="Submit" />
</form>
}
?>
If it does work, slowly work your way into your current form setup to see what is breaking it. If it doesn't work, there's something larger at play.
There are 2 things you should do now.
Remove the JavaScript function to submit the form. It's not required (or necessary). The default behavior of a submit button is to... well... submit. You don't need to help it with JavaScript.
Enable error display by using error_reporting(E_ALL).
After you do both things, you should be able to debug and assess the problem much more easily.
Put your php code inside php tags!
<?php
foreach($_POST as $field => $value)
{
echo $field ." = ." $value.'<br />';
}
?>
If you do
<?php
print_r($_POST);
?>
what do you get?
If this still doesn't work, does your server parse php?
Create the file test.php and access it directly http://localhost/test.php or whatever your URL is
<?php
echo 'Hello';
?>
if this doesn't work..it's a whole diferent problem
You can submit an HTML form using PHP with fsubmit library.
Example:
require_once 'fsubmit.php';
$html = "<form action ='res.php' method='post'><input type='text' name='name'/></form>";
$form = new Fsubmit();
$form->url = 'http://submit_url_here.com';
$form->html = $html;
$form->params = ['name'=>'kokainom'];
$response = $form->submit();
echo $response['content'];

Categories