i have a form based on database mysql ,just like this
<form name="" id="form1" method="GET" action="submit.php"></form>
<?php
include 'connection.php';
$myquery="SELECT * FROM `tbl` " ;
$params=mysql_query($myquery) or die (mysql_error());
while ($param=mysql_fetch_assoc($params))
{?>
<input name="<?php echo $param['id'] ?>" value="<?php echo $param['child_id'] ?>" />
<input type="checkbox" name="checkbox<?php echo $param['id'] ?>" value="Yes" form="form1" />
<input class="form-control text-center" form="form1" name="desc<?php echo $param['id'] ?>" value="<?php echo $param['desc'] ?>"/>
<input class="form-control text-center" form="form1" name="value <?php echo $param['id'] ?>" value="<?php echo $param['value'] ?>"/>
<select class="form-control" form="form1" id="" name="select<?php echo $param['id'] ?>">
<option value=""></option>
<option value="OK">OK</option>
<option value="NOK">NOK</option>
</select>
<?php } ?>
<button type="submit" class="btn btn-success btn-s-md btn-rounded" form="form1"><i class="icon-save"></i>Save</button>
how can i submit (do update ) to mysql database with this form , i am a newbie in php .
thanks
So yes you have to add the balise form at this end. Then I advice you to add a hidden value with your id.
<input type="hidden" name"id" value="<?php echo $param['id'] ; ?>">
After ad in your page submit.php,
<?php
include 'connection.php';
if (isset($_POST["id"])){
$id=$_POST["id"];#so you can recover your id
$myquery="UPDATE ... " ;
mysql_query($myquery) or die (mysql_error());
echo "update success";# you can add whatever you want here
}?>
You have to wrap all your form elements into the <form> tag. So place your closing tag (</form>) at the end of your form.
<form name="" id="form1" method="GET" action="submit.php">
<input name="..." />
<select>
<option value="...">...</option>
...
</select>
<button>...</button>
</form>
By the way: PHP's mysql-functions are deprecated. Use mysqli instead!
Related
I wish to display the record to the user that I am deleting in PHP?
I am trying to display the record in a form that I want to delete but only the input type = text values are being displayed and the radio button values are not being displayed?
<?php
require('/home/s3022041/sqlC/dbConnect.php');
if(isset($_POST['search']))
{
$search = mysqli_real_escape_string($connection, $_POST['id']);
$id = $_POST['id'];
$stp1 = preg_replace("/[^a-zA-Z0-9]/", "", $id); //grab only the alphanumerics
$stp2 = strtoupper($stp1); //Make all alphabets uppercase
$stp3 = preg_replace('/\d+/', '',$stp2); //extract the alphabets part
$newsearchid = str_replace($stp3,"-".$stp3."-",$stp2); //put hyphens before and after the alphabet part
$query = "SELECT * FROM cars WHERE Registration_Number = '$newsearchid' ";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run))
{
?>
<form action="delete_process.php" method="POST" class="form">
<h2>Car Registration Form</h2>
<div class="mb-3">
<input type="radio" name="Car" id="Toyota" value=" <?php echo $row['make']; ?>" />
<label for="Toyota" class="form-label">Toyota</label>
<select name="Models[Toyota]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="Camri">Camri</option>
<option value="Corolla">Corolla</option>
<option value="Estima">Estima</option>
</select><br>
<input type="radio" name="Car" id="Nissan" value=" <?php echo $row['make']; ?>"/>
<label for="Nissan" class="form-label">Nissan</label>
<select name="Models[Nissan]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="Micra">Micra</option>
<option value="roller">roller</option>
<option value="fushe">fushe</option>
</select><br>
<input type="radio" name="Car" id="Mercedez" value=" <?php echo $row['make']; ?>"/>
<label for="Mercedez" class="form-label">Mercedez</label>
<select name="Models[Mercedez]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="5series">5series</option>
<option value="x7">x7</option>
<option value="x5">x5</option>
</select><br>
<label for="vehicleidentificationnumber" class="form-label">vehicle identification
number(VIN)</label>
<input type="text" disabled="disabled" name="vehicleidentificationnumber" class="form-control" maxlength=20 value=" <?php echo $row['VIN']; ?>" required >
<br>
<label for="ManufacturingYear" class="form-label">Manufacturing Year</label>
<input type="text" name="Manufacture_Year" class="form-control" maxlength=20 value=" <?php echo $row['Manufacture_Year']; ?>" required >
<label for="Enginesize" class="form-label">Engine size</label>
<input type="text" name="Engine_Size" class="form-control" maxlength=20 value=" <?php echo $row['Engine_Size']; ?>" required>
<label for="TransmissionType" class="form-label">TransmissionType</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="Automatic" class="form-label">Automatic</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="Manual" class="form-label">Manual</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="SemiAutomatic" class="form-label">Semi-Automatic</label><br>
<label for="NoofSeats" class="form-label">No. of Seats</label>
<input type="number" name="NoOfSeats" class="form-control" value="<?php echo $row['NoOfSeats']; ?>" maxlength=20 required>
<label for="Noofdoors" class="form-label">No. of doors</label>
<input type="number" name="NoOfDoors" class="form-control" maxlength=20 value="<?php echo $row['NoOfDoors']; ?>" required>
<label for="Fueltype" class="form-label">Fuel type</label>
<input type="text" name="Fuel_Type" class="form-control" maxlength=20 value=" <?php echo $row['Fuel_Type']; ?>" required>
<label for="Colour" class="form-label">Colour</label>
<input type="text" name="Colour" class="form-control" maxlength=20 value=" <?php echo $row['Colour']; ?>" required>
<label for="RegistrationNumber" class="form-label">Registration Number (use Dublin
registration) </label>
<input type="hidden" disabled="disabled" name="Registration_Number" class="form-control" maxlength=20 value=" <?php echo $row['Registration_Number']; ?>" required>
<label for="Dateoffirstregistration" class="form-label">Date of first registration</label>
<input type="date" name="DateOfRegestration" class="form-control" maxlength=20 value=" <?php echo $row['DateOfRegestration']; ?>" required>
</div>
<button type="submit" name="search" class="btn btn-primary">Delete</button>
</form>
<?php
}
}
else{
echo "<h1> No records found </h1>";
echo "<a href='index.php'>home</a>";
}
?>
</div>
<div class="modal-footer">
</form>
Here I am trying to delete the above record that is displayed but it says deleted successfully but when I go and check the records it is not deleted it's still there?
<?php
require('/home/s3022041/sqlC/dbConnect.php');
if(isset($_POST['search']))
{
$search = mysqli_real_escape_string($connection, $_POST['search']);
$id = $_POST['Registration_Number'];
$query = "DELETE FROM `cars` WHERE Registration_Number='$id' ";
$query_run = mysqli_query($connection, $query) or die ("not done");
if($query_run)
{
echo "<h1> deleted successfully</h1>";
echo "<a href='index.php'>home</a>";
}
else
{
echo "<h1> not deleted </h1>";
echo "<a href='index.php'>home</a>";
echo 'Error! ' . mysqli_error($connection);
}
}
Change
1. Form
<input type="radio" name="Car" id="Toyota" value="<?=$row['make']; ?>" />
to
<input type="radio" name="Car" id="Toyota" <?=($row['make'] == 'Toyota' ? 'value="'.$row['make'].'" checked':NULL); ?> />
<!-- or better yet -->
<input type="radio" name="Car" id="Toyota" value="Toyota" <?=($row['make'] == 'Toyota' ? 'checked':NULL); ?> />
2. Database
File: delete_process.php
// Change
if($query_run) // << This only tell the script the sql statement run with no errors
// To
if(mysql_affected_rows()<=1) // << This confirms that one or more rows where changed by the sql statement
Explained
PHP Shorthand If/Else Using Ternary Operators (?:)
/* basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
Reason for error
If a radio button are not being displayed, it most-likely an 'Undefined variable' when getting $row['make'].
Other thing, remove the white spacing from. As this may play a part when using a MYSQL Statement.
<!-- From -->
value=" <?php echo $row['Registration_Number']; ?>"
<!-- To -->
value="<?=$row['Registration_Number']; ?>"
*Shorthand one line echo/print <?=
I am executing an form based on PHP on Windows, however after I select an item in the drop down menu, the text does not show up (even though it was selected properly, as I can verify in the database after clicking the insert button).
The form code:
<div id="janela_formulario">
<form action="inserir_transportadoras.php" method="post">
<input type="text" name="nometransportadora" placeholder="Nome da Transportadora">
<input type="text" name="endereco" placeholder="Endereço">
<input type="text" name="telefone" placeholder="Telefone">
<input type="text" name="cidade" placeholder="Cidade">
<select name="estados">
<?php
while($linha = mysqli_fetch_assoc($lista_estados)) {
?>
<option value="<?php echo $linha["estadoID"]; ?>">
<?php echo utf8_encode($linha["nome"]); ?>
</option>
<?php
}
?>
</select>
<input type="text" name="cep" placeholder="CEP">
<input type="text" name="cnpj" placeholder="CNPJ">
<input type="submit" value="inserir">
</form>
</div>
The dropdown menu PHP code:
<input type="text" name="cidade" placeholder="Cidade">
<select name="estados">
<?php
while($linha = mysqli_fetch_assoc($lista_estados)) {
?>
<option value="<?php echo $linha["estadoID"]; ?>">
<?php echo utf8_encode($linha["nome"]); ?>
</option>
<?php
}
?>
</select>
How can I solve it?
while($linha = mysqli_fetch_assoc($lista_estados)) {
echo '<option value="'. $linha["estadoID"] .'"> '.utf8_encode($linha["nome"]).' </option>';
}
Use above code instead of while loop
test by inspect HTML code. is this estadoID are printing properly if yes then test you are catching (estados) data properly
I had to retrieve data from the database (MySQL) to drop-down list in PHP. Now, I wanna retrieve data from drop-down list into the textbox.
This is my code.
<form method="POST">
<select name="searchtitle" id="drp_dwn">
<?php
$records = $conn->query("SELECT titles from products");
if ($records->num_rows > 0) {
while ($row = $records->fetch_assoc()) {
echo "<option value=\"\"> ".$row['titles']."</option>";
$titles1 = $row['titles'];
}
}
?>
</select>
<input type="submit" class="button" value="Show">
<input type="text" id="textArea" class="product-new-textbox" name="searchtitle" value='<?php echo $row['titles']; ?>' />
</form>
I attach with images.
I hope you can support me.
Thanks
So, add an empty action attribute to the form and populate the new input with the title selected when the form has been submitted. Also change the last inputs name as searchtitle is already being used by the select and add a selected attribute to the option element:
<form action="" method="post">
<select name="searchtitle" id="drp_dwn">
<?php
$records = $conn->query("SELECT titles from products");
if ($records->num_rows > 0)
{
while ($row = $records->fetch_assoc())
{
$selected = '';
if ($row['titles'] === $_POST['searchtitle'])
{
$selected = 'selected';
}
echo "<option " . $selected . " value=\"" . $row['titles'] . "\"> " . $row['titles'] . "</option>";
}
}
?>
</select>
<input type="submit" class="button" value="Show">
<?php
if (!empty($_POST))
{
?>
<input type="text" id="textArea" class="product-new-textbox" name="searchtitle1" value="<?php echo $_POST['searchtitle']; ?>" />
<?php
}
?>
</form>
<form method="POST">
<select name="searchtitle" id="drp_dwn">
<?php
$records = $conn->query("SELECT titles from products");
if ($records->num_rows > 0) {
while ($row = $records->fetch_assoc()) {
echo "<option value=\"\"> ".$row['titles']."</option>";
$titles1 = $row['titles'];
}
}
?>
</select>
<input type="submit" id="submit" class="button" value="Show">
<input type="text" id="textArea" class="product-new-textbox" name="searchtitle" value='<?php echo $row['titles']; ?>' />
</form>
simple JS code
<script>
$(document).on("click","#submit",function(){
var x = $("#drp_dwn").val();
$("#textArea").val(x);
});
</script>
Try this. Do you want to show the dropdown value in the text box? This code will show the dropdown value in the text box on changing the dropdown value and on click the show button.
I have created a showText() function which you can call anywhere either on click the show button or on change the dropdown. You can change the dropdown option values by using your php while loop. Here is the example code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST">
<select name="searchtitle" id="drp_dwn" onchange="showText()">
<option value="one" >One</option>
<option value="Two" >Two</option>
<option value="Three" >Three</option>
</select>
<input type="button" onclick="showText()" class="button" value="Show">
<input type="text" id="textArea" class="product-new-textbox" name="searchtitle" value='' />
</form>
<script>
function showText()
{
$("#textArea").val($("#drp_dwn").val());
}
</script>
You can also use
<form method="POST" action="" >
<select name="searchtitle" id="drp_dwn">
<option value="one" >One</option>
<option value="Two" >Two</option>
<option value="Three" >Three</option>
</select>
<input type="submit" onclick="showText()" class="button" value="Show">
<input type="text" id="textArea" value="<?php if(isset($_POST['searchtitle'])){ echo $_POST['searchtitle']; } ?>" class="product-new-textbox" name="searchtitle" value='' />
</form>
OR you can use get method as well
<form action="" >
<select name="searchtitle" id="drp_dwn">
<option value="one" >One</option>
<option value="Two" >Two</option>
<option value="Three" >Three</option>
</select>
<input type="submit" onclick="showText()" class="button" value="Show">
<input type="text" id="textArea" value="<?php if(isset(#$_GET['searchtitle'])){ echo $_GET['searchtitle']; } ?>" class="product-new-textbox" name="searchtitle" value='' />
</form>
I have a form in html and I am saving it as search.php:
<form name="myform" action="" method="POST" onsubmit="search_clicked(); return false;">
Keyword<input type="text" name="Keyword" id="Keyword" value="XYZ" required/><!-- value is the default name that appears in the text box-->
<br>
Type
<select name="sel" id="sel" class="form-control" onchange="checkcolors(this.value)">
<option selected value="Users">Users</option>
<option value="Pages">Pages</option>
<option value="Events">Events</option>
<option value="Places">Places</option>
<option value="Groups">Groups</option>
</select>
</form>
<div id="loc_dist_displayarea" style="display:none;">
Location<input type="text" name="Location" value="90007" required/> Distance(meters)<input type="text" name="distance" value="10000" required/>
</div>
<br><br>
<input type="submit" name="Search"/>
<input type="submit" value="clear" id="clear" onclick="return clearclicked()"/>
</form>
and my php script is in the same file:
<div id="body_area" style="display:none">
<?php
echo "hi I am searching ";
if($_SERVER["REQUEST_METHOD"]=="POST")
{
//echo "yes value is selected";
//echo $_POST["Keyword"];
if (isset($_POST['sel'])) {
$selectedval= $_POST["sel"];
echo "$selectedval";
}
//echo $_POST["Location"];
}
echo "no value is selected";
?>
</div>
I am not able to display the $_POST['sel'] while $_POST['Keyword'] is echoed.Please help.
First of all, you arent using good programming practices, you use quotation marks (these " and these ') Indiscriminately. You should only alternate between them when you have them nested.
Next, on the action paramenter you should put the name of the file, even if it's the same.
<form action="<?php echo $self ?>" method="post">
<br>
<select name="choice" id="choice">
<option value="product" name="product">Product</option>
<option value="saab">Brand</option>
<option value="mercedes">Price</option>
<option value="audi">Name</option>
</select>
<br>
search :<input name="<?php $choice=$_POST['choice'];?>" type="text" value="<?php $choice ?>"><br>
<br><input type="submit" name="Submit" value="Search">
</form>
What i want to do is get the value from my dropdown list and set the to the value of my textfield dont want to use java script
i want the value selected from "choice" to be the name of my textarea search seen below the
Anyhelp is greatly apperciated
thanks
$self = $_SERVER['PHP_SELF'];
if (isset($_POST['Submit'])) {
$prod = trim(isset($_REQUEST['prod'])) ? $_REQUEST['prod'] : '';
$brand = trim(isset($_REQUEST['brand'])) ? $_REQUEST['brand'] : '';
$prodname = trim(isset($_REQUEST['prodname'])) ? $_REQUEST['prodname'] : '';
$price = trim(isset($_REQUEST['price'])) ? $_REQUEST['price'] : '';
if(isset($_REQUEST['userid'])){
$userid = $_REQUEST['userid'];
}
echo $userid;
<form action="<?php echo $self ?>" method="post">
<br>
<select name="userid" id="userid" >
<option value="Criteria" name="Criteria">Select Criteria</option>
<option value="prod" name="prod">Product</option>
<option value="brand" name="brand">Brand</option>
<option value="price" name="price">Price</option>
<option value="prodname" name="prodname">Name</option>
</select>
<br>
search :<input name="<?php echo $_REQUEST['userid']; ?>" type="text" value="<?php echo $_REQUEST['userid']; ?>" ><br>
<br><input type="submit" name="Submit" value="Search">
</form>
Reading your comment
Because i want my . to search my database according to what criteria is selected in the dropdown menu
I will still say that you do not need <input name="<?php $choice=$_POST['choice'];?> in your code ...
When the Form is submitted, the selected value of that select list is also submitted with form. As your <select>'s name attribute value is choice, you can get the selected value with $_POST['choice'] in your PHP script.
<form action="<?php echo $self ?>" method="post">
<br>
<select name="choice" id="choice">
<option value="product" name="product">Product</option>
<option value="saab">Brand</option>
<option value="mercedes">Price</option>
<option value="audi">Name</option>
</select>
<?php
$value_to_show = (isset($_POST['choice']) && !empty($_POST['choice'])) ? $_POST['choice'] : "";
?>
<input name="<?php echo $value_to_show ?>" type="text" value="<?php echo $value_to_show ?>"><br>
<br>
<input type="submit" name="Submit" value="Search">
</form>
For example. If I select <option value="saab">Brand</option> and submit the form, then $_POST['choice'] value will be saab
Hope it helps