Change textbox based on select - php

I'm trying to develop a little project and I got a little problem
I'm making a PHP page connected to a MySQL Server. I have a page where only the administrator have access and that page allows him to change users information.
So, I have a select that show all usernames on website and 3 textbox where can be inserted the First Name, Last Name and e-mail.
My database update successfully, but what i'm trying to do is when is select a username on select the 3 textbook auto fill with that username information.
Here is an image that explains better what I would like to do.
Sorry for my english and thanks to all
http://s3.postimg.org/da9srvh5f/table_copy.jpg

<script type="text/javascript">
function reload() {
var val=document.getElementById("c-name").value;
self.location="your_url.php?name=" + val ;
//alert(val);
}
</script>
<form name="data" action="your_url.php" method="post">
<table id="tbl">
<tr>
<td>Name </td>
<td>
<select id="c-name" name="name" onChange="reload()" style="width:340px;">
<option value="">Select Name</option>
<option value="abc">ABC</option>
<option value="xyz">XYZ</option>
<option value="mno">MNO</option>
</select>
</td>
</tr>
<?php
if(isset($_GET['name'])){
$name = $_GET['name'];
$SQL_Data = "select * from client_data where cname = '".$name."'";
$result = mysqli_query($SQL_Data);
while($details = mysqli_fetch_array($result)) {
?>
<tr>
<td>First Name</td>
<td><input type="text" name="fname" class="c1" value="<?=$details['fname']?>" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" class="c7" name="lname" value="<?=$details['lname']?>" /></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" class="c7" name="uname" value="<?=$details['uname']?>" /></td>
</tr>
<?php
} }
?>
<tr><td align="center" colspan="2"><input type="submit" name="submit" value="Submit" /></tr>
</table>
</form>

You could have your PHP backend return JSON data to your frontend. Then, with javascript, do something like:
document.getElementsById('ID').Value = VALUE;
Where ID is the Id of your select and VALUE is the value found in your JSON data.
Alternatively, you could do this purely in PHP/MySQL.

Related

Cant input data to database with PDO

im trying to input data to database with PDO,i make a form for users to input,so forminput -> inputprocess.
But i cant input coz when i press submit its going to proses_input.php and show blank page.
I try to write the code in 1 file but when i press submit it show the input form again.
Here is my form_input.php
<form method="POST" action="proses_input.php">
<table>
<tr>
<td>Nama</td>
<td> : </td>
<td>
<input type="text" name="nama" required>
</td>
</tr>
<tr>
<td>No. Pegawai</td>
<td> : </td>
<td>
<input type="text" name="no_pegawai" required>
</td>
</tr>
<tr>
<td>Bagian</td>
<td> : </td>
<td>
<input type="text" name="bagian">
</td>
</tr>
<tr>
<td>Jabatan</td>
<td> : </td>
<td>
<input type="text" name="jabatan">
</td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<button type="submit" value="submit">Input</button>
</td>
</tr>
</table>
</form>
and here is my proses_input.php
<?php
include_once "connect.php";
if (isset($_POST["submit"])) {
$no_pegawai = $_POST['no_pegawai'];
$nama = $_POST['nama'];
$bagian = $_POST['bagian'];
$jabatan = $_POST['jabatan'];
try { //KONDISI
$sql = "INSERT INTO phonebook (no_pegawai, nama, bagian, jabatan)
VALUES (:no_pegawai, :nama, :bagian, :jabatan)";
$input = $conn->prepare($sql);
$input->bindparam(':no_pegawai', $no_pegawai);
$input->bindparam(':nama', $nama);
$input->bindparam(':bagian', $bagian);
$input->bindparam(':jabatan', $jabatan);
$input->execute();
header('location: admin.php');
} catch (PDOException $e) {
echo $e->getMessage();
}
}
?>
IDK why but everytime i input data to my form,its always stuck on proses_input.php with blank page.i already try to put it in one php file but still not works. Pls help me,thanks
As mentioned above you haven't named your button, therefore it won't match the condition isset($_POST["submit"]) because there is no field named "submit" in your $_POST array. Also worth noting that you should not rely on users input. I saw that you are using parameterized query which is nice but don't forget to escape these values when displaying them on the UI to avoid XSS vulnerabilities.

how to display data from mysql, on tables that have been created in the .html file

I am confused how to display data that has been entered into mysql by
via file.php to be displayed into the existing table in file.html
here is the code .html :
<form class="formnya" action="../php/newcustomer.php" method="POST" enctype="multipart/form-data">
<table class="form_new_customer">
<tr>
<th colspan="2"><h3 align="center"><u>Customer Baru</u></h3></th>
</tr>
<tr>
<td>Nama Lengkap:</td>
<td><input type="text" name="nama_lengkap"></td>
</tr>
<tr>
<td>Jenis Kelamin:</td>
<td>
<input type="radio" name="gender" value="Pria">Pria
<input type="radio" name="gender" value="Wanita">Wanita
</td>
</tr>
<tr>
<td>Nama Toko:</td>
<td><input type="text" name="nama_toko"></td>
</tr>
<tr>
<td class="alamat">Alamat:</td>
<td><textarea rows="5" cols="21" name="alamat"></textarea></td>
</tr>
<tr>
<td>No.Hp/Telp :</td>
<td><input type="text" name="no_hp"></td>
</tr>
<!-- <tr>
<td>Foto KTP: </td>
<td><input type="file" name="files"></td>
</tr> -->
<tr>
<td></td>
<td><input type="submit" name="submit" class="btn1"></td>
</tr>
</table>
</form>
and below is my .php file
<?php
if(isset($_POST['submit'])){
include_once 'connect.php';
$nama_lengkap = $_POST['nama_lengkap'];
$gender = $_POST['gender'];
$nama_toko = $_POST['nama_toko'];
$alamat = $_POST['alamat'];
$no_hp = $_POST['no_hp'];
// $files = $_FILES['files'];
$sql = "INSERT INTO user ('nama_lengkap','jenis_kelamin','nama_toko','alamat','no_handphone')
VALUES ('$nama_lengkap','$gender','$nama_toko','$alamat','$no_hp')";
$result = $conn->query($sql);
header("Location: ../pages/newcustomer.html?=success");
}
?>
so my question is, may i used the file .html to show the data from .php?
You can use Ajax (use another PHP file) to load data from database and then display in your HTML file.
But, you can also convert your .html file to .php file in order to get data from MySQL directly.
For Ajax based data, please refer to this example:
https://www.w3schools.com/pHP/php_ajax_php.asp
From the ajax URL (a PHP file), you can return data in JSON format or a complete HTML based table. Better is to return JSON based data, then parse the data in your JavaScript code and insert the data in your HTML table in code.html
PS: You can call Ajax function on document/body onload event like:
<head>
<script>
function getData() {
// Your ajax code here (URL given above for example code)
}
</script>
</head>
<body onload="getData()">
...
</body>

How to pass a variable to another page in php using GET or POST method?

I am making an editing form.
There are 2 forms, one form called edit1.php and another form called edit.php. This second one should get the id generated from the edit1.php and use it to generate further queries. The following is the code for the 2 files:
edit1.php:
<table>
<tr>
<td align="center"><h4><b>EDIT DATA</b></h4></td>
</tr>
<tr>
<td>
<table border="3">
<form action="edit.php" method="get">
<?php
$vendors = "SELECT `vendor`.`v_id`,`vendor`.`v_name` FROM `stfood`.`vendor`";
$result = mysqli_query($con, $vendors);
while ($row = mysqli_fetch_array($result)){
echo ("<tr><td>'".$row["v_id"]."'</td>");
echo ("<td>'".$row["v_name"]."'</td>");
echo ("<td>Edit</td></tr>");
}
?>
</form>
</table>
edit.php:
<?php
require 'init.php';
require 'functions.php';
?>
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>Form Edit vendor Data</td>
</tr>
<tr>
<td>
<table>
<?php
echo $_REQUEST['v_id'];
$vendor = "SELECT `vendor`.`v_name`, `vendor`.`v_email`,`vendor`.`v_desc`,`vendor`.`cont_id` FROM `stfood`.`vendor` WHERE `vendor`.`v_id` = '".$v_id."'";
$vendor_result = mysqli_query($con, $vendor);
$row = mysqli_fetch_array($vendor_result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo $row["v_id"];?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name" size="20" value="<?php echo $row["v_name"]; ?>">
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input type="text" name="email" size="50" value="<?php echo $row["v_email"];?>">
</td>
</tr>
<tr>
<td>Vendor Description</td>
<td>
<input type="text" name="description" size="100" value="<?php echo $row["v_desc"];?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
When I run the code, the first form displays all the relevant data, and when I click on the edit link the page gets redirected and I can see the v_id passed in the URL, but nothing comes into the edit.php file. When I do a var_dump($row['v_id']); I get NULL.
Why is v_id not set in edit.php?
Since your hyperlink looks like this:
Edit
Your edit.php should use
echo $_GET['id'];
// or something like this
$received_id = $_GET['id'];
// do validation whether you received a good id or not and then move on
...
...
<input type="hidden" name="id" value="<?php echo $received_id;?>">
on your edit.php page you have:
Edit
now on your edit1.php you should first have a isset() and then as follow:
if(isset($_GET['id']){
$id = $_GET['id'];
//carry on blah3 ...
In the edit1.php you have not used form elements. The value $row['v_Id'] should be the value of form element input like this inside form
<input type='hidden' name='v_I'd value ='<?php echo $row['v_id'] ?>'>
Try using $_GET['id'] instead of $_REQUEST['v_id'] on edit.php
See example #1 here
Also, have you defined the $v_id before using it in the query?
Like this.

Fill the fields of a form from mysql query result

In my project it has an interface(designation.php) which has a form. When a user enters a 'designation code'(which is a primary key) and click the 'update' button, the
remaining fields should be filled accordingly. I understand that Ajax will be needed for this.
but the problem is, i'm done with retrieving the result only as a single row from the database by using following query. but, this is not my objective.
while($row = mysqli_fetch_assoc($run1)) {
echo "<tr id='tr'>";
foreach($row AS $cell) echo "<td>$cell</td>";
echo "</tr>\n";
}
Following is the 'designation.php'..
<form action="crud.php" method="post">
<table>
<th >DESIGNATION</th>
<tr>
<td >Designation Code</td>
<td ><input type="text" name="des_code" /></td>
</tr>
<tr>
<td >Designation</td>
<td><input type="text" name="desig" /></td>
</tr>
<tr>
<td >Salary Code</td>
<td><input type="text" name="s_code" /></td>
</tr>
<tr>
<td >Salary Scale</td>
<td><input type="text" name="s_scale" /></td>
</tr>
<tr>
<td >Salary point </td>
<td><input type="text" name="s_point" /></td>
</tr>
<tr>
<td>Date</td>
<td > <input type="date" name="date" /></td>
</tr>
<tr>
<td><input type="submit" name="update" value="Update" /></td>
</tr>
</table>
I attempted many solutions but have not gotten the intended result. Any help is appreciated.
$(document).ready(function(){
("#form1").submit(function({
var id=<?php echo $_POST['des_code']; ?>;
$.ajax({
url: 'update.php',
type:'POST',
data: {id:id},
success : function (data)
{
var a=data.split('[BRK]');
$("#s_scale").val(a[0]);
// like wise store all your fields..
}
})
})
upadte.php will contain
$id=$_POST['id']; //designation id from jqyery
$q=mysqli_query("your goes here..");
while($row = mysqli_fetch_array($q))
{
$cell=$row[0];
$cell.="[BRK]".$row[1];
$cell.="[BRK]".$row[2];
}
echo $cell;
give id to your text field so from jquery you can add value directly.. and give form id also so you can fire onsubmit event...
i didn't try this code but hope it will work fine...
To transfer more than one value use json!
Jquery Code::
$(function(){
id="XXXX";
$("form").on("submit",function(e){
e.preventDefault();
$.getJson("update.php",{"id":id},function(response){
$("#value1").val(response.value1);
$("#value2").val(response.value2);
// like wise store all your fields..
})
})
})
Update.php::
$q=mysqli_query("your query");
$row = mysqli_fetch_array($q);
echo json_encode($row);
$row will be an array containing value1, value2 and so on!!

How to show the result when I click the submit button? (Like View history)

Here's the concept. The users will input the data from the text bar.. And when click the submit button, the data will be saved to the "VIEW HISTORY" menu bar. View history is where the data's inputted from the home.php are being made.
E.g:
Home.php
(data1)
Enter Name: (I type..)Black Cat
Age: (I type..)21
Job: (I type..)Spy
(data2)
Enter Name: (I type..)Black Dog
Age: (I type..)24
Job: (I type..)Cook
Submit Button (I click it) and it says the data is submitted to my database.
Then when I click the "View History Menu Bar".. The ff will be shown:
DATA1
DATA2
<Click next to view next page.. etc>
When I click data1, the whole name, age and job from data1 will appear. And so is from data2.. and so on.. :)
That's my only problem.. I can't show those data :( Please help! I have some codes in here.. But I dunno how to fix it :( I hope you can give some samples so I can imitate the moves, or better if we help on fixing this. :( THANKS Y'ALL!
<?php
$cfccon = include("E:/xampp/conn1/cfmscsd.php");
//This is about the localhost, username, password stuff
$query="SELECT * FROM contents";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$B1=mysql_result($result,$i,"B1");
$B2=mysql_result($result,$i,"B2");
$B3=mysql_result($result,$i,"B3");
$B4=mysql_result($result,$i,"B4");
$B5=mysql_result($result,$i,"B5");
$B6=mysql_result($result,$i,"B6");
$B7=mysql_result($result,$i,"B7");
$B8=mysql_result($result,$i,"B8");
$B9=mysql_result($result,$i,"B9");
$B10=mysql_result($result,$i,"B10");
$i++;
}
?>
<html>
<tr>
<td bgcolor="#bfb9a7"><span class="style77">CCC</span></td>
<td bgcolor="#CCCCCC"><div align="center" class="style66"><input name="B1" type="text" size="18" id="B1" value="<?php echo $B1;?>" disabled="disabled"/></div></td>
<td bgcolor="#CCCCCC"><div align="center" class="style66"><input name="B2" type="text" size="18" id="B2" value="<?php echo $B2;?>" disabled="disabled"/></div></td>
<td bgcolor="#CCCCCC"><div align="center" class="style66"><input name="B3" type="text" size="18" id="B3" value="<?php echo $B3;?>" disabled="disabled"/></div></td>
<td bgcolor="#CCCCCC"><center><div class="style66"><input type="button" onclick="tbsp1()" value="<?php echo $tbsp1;?>add" disabled="disabled"/></center></td>
<td bgcolor="#FEEDD8"><div align="center" class="style66"><input name="B4" type="text" size="18" id="B4" value="<?php echo $B4;?>" disabled="disabled"/></div></td>
</tr>
<tr>
<td><span class="style77">SCC</span></td>
<td><div align="center" class="style66"><input name="B5" type="text" size="18" id="B5" value="<?php echo $B5;?>" disabled="disabled"/></div></td>
<td><div align="center" class="style66"><input name="B6" type="text" size="18" id="B6" value="<?php echo $B6;?>" disabled="disabled"/></div></td>
<td><div align="center" class="style66"><input name="B7" type="text" size="18" id="B7" value="<?php echo $B7;?>" disabled="disabled"/></div></td>
<td><center><input type="button" onclick="tbsp2()" value="<?php echo $tbsp2;?>add" disabled="disabled"/></center></td>
<td bgcolor="#FEEDD8"><div align="center"><input name="B8" type="text" size="18" id="B8" value="<?php echo $B8;?>" disabled="disabled"/></div></td>
</tr>
<tr>
<td bgcolor="#bfb9a7"><span class="style77">NCC</span></td>
<td bgcolor="#CCCCCC"><div align="center" class="style66"><input name="B9" type="text" size="18" id="B9" value="<?php echo $B9;?>" disabled="disabled"/></div></td>
<td bgcolor="#CCCCCC"><div align="center" class="style66"><input name="B10" type="text" size="18" id="B10" value="<?php echo $B10;?>" disabled="disabled"/></div></td>
</tr>
</html>
First of all, I recommend that you rename your table columns, B1, B2, B3 etc are very very bad names, instead use something descriptive like name, age, email, etc. That will make your code a lot easier to read and debug.
To achieve what you're asking for, do something like this:
1) When the user clicks the submit button, do a form post to a script on your site, e.g
<form method=post action=home.php>
Enter data: <input type=text name=data>
<input type=submit value=Submit />
</form>
2) When the user clicks the submit button, you could get the data he posted in $_POST, e.g $_POST['data']. Save this to a mysql database. You should give the user a unique user ID. Each row in your table should have these fields:
dataId (int,primary, auto_increment)
userID (int)
name (varchar 255)
age (int)
etc...
Then you should have a different table for users, called a user table. Each user who visits the website should have a unique userId. You can store this userId in $_SESSION, I recommend you read about session handling.
Then whenever the user submits his form, you add a new row to your data table with his info, and the userId field with the given user's id.
3) When the user clicks 'next' to see the data, you would do something like this:
$userId = $_SESSION['userId'];
$sql = "SELECT * FROM data WHERE userId='$userId'";
$result = mysql_query($sql) or die(mysql_error());
$history = array();
while ($row = mysql_fetch_assoc($result))
{
$history[] = $row;
}
4) Then simply loop through the $rows using foreach and display the history.

Categories