Currently, I have an index.php file with a form and a text input from the user as well as a submit button. When the submit button is pressed, the process.php file is supposed to get the data and echo it out. However, it just sends me to a blank page and does not echo anything out. I am well aware that I would need to style it the page, etc... But it just isn't echoing out at all. I am already connected to the mySQL DB with another php script and have tested that and it works fine so I know I am connected. What am i doing wrong?
index.php
<form action="process.php" form method="post" id="myForm">
<div class="col-xs-12 col-md-6">
<div class="input-group">
<span class="input-group-addon">
<input aria-label="..." type="checkbox" id="checkbox1">
</span>
<input aria-label="..." class="form-control" type="text" id="food1">
</div>
</div>
<input type="submit" value="Submit">
</form>
process.php
<?php
//Check whether the form has been submitted
if($_POST['submit'] == "Submit")
{
$varFood1 = $_POST['food1'];
$sql = "SELECT menu.dish FROM menu WHERE menu.description LIKE '%varFood1%'"; // sql query
$result = mysql_query($sql);
// Loop the recordset $result
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($result)) {
echo "Items : {$row['dish']}\n";
}
}
?>
Use name attribute not id. <input aria-label="..." class="form-control" type="text" id="food1"> Try printting out the POST in the future.
<input aria-label="..." class="form-control" type="text" id="food1" name="food1">
Additional changes...
$varFood1 = mysql_real_escape_string($_POST['food1']);
$sql = "SELECT menu.dish FROM menu WHERE menu.description LIKE '%$varFood1%'";
without escaping you open yourself to injections. You should consider switching driver to mysqli or PDO as well.
The problem is that you are checking for $_POST['submit'], which the process.php file does not see, because it does not exist in the POST array. Solution: give your submit button a name, instead of this:
<input type="submit" value="Submit">,
which is what you have, do this:
<input type="submit" name="submit" value="Submit">
Related
I'm trying to build email subscribtion form but it don't work for me. After click it redirects to next page but there is no data input in database. Also also it redirects after "Continue" even inputs values is blank.
Form in HTML:
<form action="https://www.next-page.com" method="post">
<input type="text" name="name" placeholder="Your name" id="name"/>
<input type="text" name="email" placeholder="E-mail" id="email"/>
<label for="tac">
<input type="checkbox" id="tac"/>
<span class="concheck">I have read and agree to the <a class="tac">terms and conditions</a></span>
<span class="please">Please, agree our terms and conditions</span>
</label>
<button class="continue" type="submit" name="submit"><span>+</span> Continue</button>
<button class="carga"><img src="img/loading.gif"/></button>
</form>
PHP in HTML:
<?php
require_once "db.php";
if(isset($_REQUEST['submit']))
{
mysqli_query($con, "INSERT INTO database (name, email) VALUES ('".$_POST["name"]."', '".$_POST["email"]."')");
$_POST["name"];
$_POST["email"];
header("Location: https://www.next-page.com");
}
?>
DB.php
<?php
$con = mysqli_connect("localhost","username","password","databasename");
mysqli_set_charset($con,"utf8");
?>
If you have this two codes into one file for example like index.php then you should remove https://www.next-page.com from action
<form action="/" method="post">
I think it should work.
And also there should be
if(isset($_POST['submit']))
action should go for the PHP that will validate, so create a new PHP script that will validate it, insite put the method you want to use $_GET, or $_POST if this don't work, please use PHP + name.php on CMD/terminal and post the error here, if you trying to do this in the same page, you have to use .php on the file, and also remove the action from it.
new to PHP here
I have a page that loops through a DB and displays the contents of the table.
When the delete button is pressed I would like to delete that entry in the DB.
Right now when the button is pressed and the page goes to delete.php I keep getting Undefined index: userid
Here is the first page:
<?php
foreach($rows as $row){
$userid = $row['userid'];
$pName = $row['pName'];
$pDesc = $row['pDesc'];
$dDate = $row['dDate'];
?>
<div class="project-container">
<label>Project ID:</label>
<span><?php echo $userid; ?></span><br>
<label>Project Owner:</label>
<span><?php echo $pName; ?></span><br>
<label>Project Description:</label>
<span><?php echo $pDesc; ?> </span><br>
<label>Project Due Date:</label>
<span><?php echo $dDate; ?> </span><br>
<br>
<form action="#" method="GET">
<input type="submit" name="delete" value="Delete Project">
</form>
<form action="index.php">
<input type="submit" name="update" value="Update Project">
</form>
</div>
<br>
<br>
</div><br><br><?php } ?>
and this is delete.php:
include('connect.php');
$userid = $_GET['userid'];
echo $userid;
$sql = "DELETE FROM projecttable WHERE userid = '$userid'";
$conn->exec($sql);
Any help is appreciated, thank you
Try this. It will send a variable in the GET array named userid with the userid value to delete.php:
<form action="delete.php" method="GET">
<input type="hidden" name="userid" value="<?php echo $userid; ?>">
<input type="submit" name="delete" value="Delete Project">
</form>
The action attribute in the form tag will direct where to sent the request which is the delete.php script and the method will tell it to use the GET array which basically puts the keys and values in the URL itself. You could alternately do this by modifying the action field instead of using an input field. I just like this way for readability.
Because input fields are usually visible, using a hidden field prevents it from being displayed on the web page, but it's easy to manage in the code. The name attribute of the input field determines the name of the key in the GET array and the value attribute determined the value of that element in the GET array. So it ends up being $_GET['name_attribtue'] = value_attribute in your PHP script.
Just for example. If you changed the form method attribute to POST, your PHP script would need to use the $_POST array instead of the $_GET array.
I am learning PHP and have a situation where I want the method to be POST to pass variables to my php and
1) Connect to Server2) store results in a variable3) Display an HTML Table
However, I later on in my syntax want to use GET to "recall" that variable and output the results to a CSV file. This is what I have, but when I click the button Generate nothing happens. Is this possible? If not, how should I re-write the syntax to make ithappen?
<body>
<form method="POST">
End Date:<input type="date" name="end" value="<?= $_POST['end'] ?>">
<input type="submit" name="submit" value="Go">
</form>
</body>
<?php
if (isset($_POST['submit']))
{
//Connect To Server
//Store Result Set in Variable
//Display HTML Table
}
?>
<form method="get">
<button type="submit" name="csv" value="1">Generate CSV</button>
</form>
Hi guys please help me here i'm to submit my posts on DHIVEHI language but sql database shows it something like this (???????&%$) what to do???? here is code some parts are written in DHIVEHI and class MVDIV is dhivehi language style class. plx help me guys?
FORM PAGE
<form enctype="multipart/form-data" action="post.php" method="post">
<label class="mvdiv" for="type">ވައްތަރު </label>
<input type="text" id="type" name="type" dir="rtl" class="thaanaKeyboardInput mvdiv form-control" >
</div>
<button name="submit" type="submit data" value="data" class="btn btn-default mvdiv">ފޮނުއްވާ</button>
</form>
POST.php page
if (isset($_POST["type"])) {
$name = crypt($_POST["type"]);
$sql = mysql_query("INSERT INTO story_type (name)
VALUES('$name')") or die (mysql_error());
echo "$name";
}
I've created a members area where a user can update their bio. The problem is that the information the user submits isn't updating the rows in the database.
Member's Area
<body bgcolor="#E6E6FA">
<button>Log Out</button><br><br>
<input type="text" name="age"placeholder="Enter a your age."><br>
<input type="text" name="bio"placeholder="Enter your bio.">
<input type="submit" name="submit" value="Submit your details!">
PHP
<?php
if(isset($_POST['submit'])){
$con=mysql_connect("localhost","root","****","****");
// Check connection
if (mysql_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$age = mysql_real_escape_string($_POST['age']);
$bio = mysql_real_escape_string($_POST['bio']);
$name = mysql_real_escape_string($_SESSION['username']);
mysql_query($con,"UPDATE accs SET age='.$age.' WHERE name='.$name.'");
mysql_query($con,"UPDATE accs SET bio='.$bio.' WHERE name='.$name.'");
mysql_close($con);
};
?>
</body></html>
Any Ideas as to what is wrong here?
in your HTML page, the form should be inside the <form></form> tags
<form method="post" action="update.php">
<input type="text" name="age" placeholder="Enter a your age.">
<br>
<input type="text" name="bio" placeholder="Enter your bio.">
<input type="submit" name="submit" value="Submit your details!">
</form>
In your PHP page - to check the results, you can temporarily echo $age; echo $bio;
As you are using $_SESSION['username']; I think you are missing session_start(); to the top of your PHP code.
Also mysql_query only needs the SQL command, and not the connection ($con), that is mysqli, which is strongly advised to use instead of mysql_*.
As a side note, don't rely on user names in your database as the update criteria. If not already introduced, you can add an ID column to your table
a) create a proper submit form. use form tags around your form fields.
b) check, that the form is correctly submitted, by checking the $_POST array.
var_dump($_POST);
c) check, that you have values for the fields that you want to insert.
do a var_dump() before mysql_query(), to see what's going on.
var_dump($age, $bio, $name);
d) combine your two query calls into one:
mysql_query($con, "UPDATE accs SET age='.$age.', bio='.$bio.' WHERE name='.$name.'");
If you want to use the page it self to process your request, then empty the action property of your form. For example :
<form method="post" action="">
<input type="text" name="age"placeholder="Enter a your age."><br>
<input type="text" name="bio"placeholder="Enter your bio.">
<input type="submit" name="submit" value="Submit your details!">
</form>