Is that possible to select variable in DB (such as : $username) and display their info in textbox ?
Example:
Select : $username
after the selection, in the text input , will show:
$username , $usernameEmail , $usernameContactNumber
and click on "Register", it will insert to database with the information
I have no idea how does this work. I have no problem in inserting value to database.
The only problem that I am unable to solve right now is the condition I needed.
When I choose $username (example: userA),
all the information of userA will be appearing in the text input below
$query = "SELECT * FROM userdatabase";
$result1 = mysqli_query($db,$query);
<select>
<?php while($row1 = mysqli_fetch_array($result1)):; ?>
<option value="<?php echo $row1[0];?>"><?php echo $row1[2];?></option>
<?php endwhile;?>
</select>
The code above is the selection (PHP
<script>
$(document).ready(function(){
$("form#get").submit(function(event) {
event.preventDefault();
var input = $("#username");
$.ajax({
type: "POST",
url: "userregisteraccount.php",
success: function(data) { input.val(data); }
});
});
});
</script>
code above is the ajax
so that when i choose "userName" , "userName" data will be show in the registerform
Related
I'm pulling data from an Azure SQL database using PHP and have successfully created a drop-down (Select) box with Options that are pulled from the database.
The SQL query returns 2 columns, Title & Cycle_ID.
I have set the Title as the text string and the Cycle_ID as the value.
I now want to store the value (Cycle_ID) of the current selection in a variable (MyVariable), which I will use in the SQL query for the next drop-down box I'm creating, i.e. WHERE Cycle_ID = MyVariable.
This way the user progressively narrows their selection as they work their way down through my drop-down boxes.
My current code is below, but I don't know how to create and store the current selection to MyVariable.
<?php
//create the database connection and define the query
$serverName = "myserver";
$connectionOptions = array(
"Database" => "mydatabase",
"Uid" => "mysqlaccount",
"PWD" => "mypassword"
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
//defines cycle query
$cyclesql= "SELECT [Cycle_ID]
,[Title]
FROM [ods].[Cycle]
WHERE End_Date > DATEADD(month,-6,GETDATE()) AND Updated IS NOT NULL
ORDER BY Cycle_ID Asc";
$cycleResults= sqlsrv_query($conn, $cyclesql);
if ($cycleResults == FALSE)
echo (sqlsrv_errors());
?>
<html>
<body>
<form action="cyclefile.php" method="post">
<div id="select">
<p>Select the week:</p><select name='Week'>
<option value="">Select...</option>
<?php
//starts loop
while ($row = sqlsrv_fetch_array($cycleResults, SQLSRV_FETCH_BOTH)) {
//defines cycle variable
$cycle = $row['Title'];
//defines cycle_id variable
$cycle_id = $row['Cycle_ID'];
//displays cycle variable as option in select (dropdown) list and cycle_id as value
echo '<option value="'.$cycle_id.'">'.$cycle.'</option>';
}
?>
</select>
</div>
</form>
</body>
</html>
I suggest you to make an ajax call on selection and pass the selected value to process your result in another file where you can run your query.
<select name="cycle_data" id="cycle_id">
<option value="cycle1" > Cycle 1</option>
<option value="cycle2" >Cycle 2</option>
</select>
Then lets do a script:
<script type="text/javascript">
$(document).ready(function(){
$("#cycle_id").change(function(){
var cycle_data = $(this).val();
var dataString = "cycle_data="+cycle_data;
$.ajax({
type: "POST",
url: "get-data.php",
data: dataString,
success: function(result){
// Process your result here
}
});
});
});
</script>
I have a basic form that I would like to have other fields appear underneath such as email address and job title when the user is selected from the drop down.
The process will be:
User selects name from the drop down
PHP, JQUERY to query mysql database where user is equal to the selection from dropdown
Email address and Job Title fields to appear underneath with populated information.
My JQuery doesn't seem to work on my page...
I have used the code from this Stack Overflow question in which my reply was removed because it was not a 'solution': Populate input fields with values when option is selected - php mysql
Any help on this would be appreciated!
self_submit.html
<html>
<head>
<title>TEST</title>
<script>
$(document).ready(function(){
$('#user').on('change',function(){
var user = $(this).val();
$.ajax({
url : "getUser.php",
dataType: 'json',
type: 'POST',
async : false,
data : { user : user},
success : function(data) {
userData = json.parse(data);
$('#age').val(userData.age);
$('#email').val(userData.email);
}
});
});
});
</script>
</head>
<body>
<form>
User
<select name="user" id="user">
<option>-- Select User --</option>
<option value="username1">name1</option>
<option value="username1">name2</option>
<option value="username1">name3</option>
</select>
<p>
Age
<input type="text" name="jobtitle" id="jobtitle">
</p>
<p>
Email
<input type="text" name="email" id="email">
</p>
</form>
</body>
</html>
getUser.php
<?php
$link = mysqli_connect("localhost", "root", "", "test");
$user = $_GET['user'];
$sql = mysqli_query($link, "SELECT jobtitle, email FROM tblusers WHERE username = '".$user."' ");
$row = mysqli_fetch_array($sql);
json_encode($row);die;
?>
Thanks in advance
you need to change your ajax call to:
$.ajax({
url : "getUser.php",
dataType: 'json',
type: 'POST',
data : { user : user },
success : function(data) {
userData = json.parse(data);
// $('#age').val(userData.age);
$('#jobtitle').val(userData.jobtitle); // Since you are selecting jobtitle, not age
$('#email').val(userData.email);
}
});
And also you forgot to echo your data:
<?php
$link = mysqli_connect("localhost", "root", "", "test");
$user = $_REQUEST['user'];
$sql = mysqli_query($link, "SELECT jobtitle, email FROM tblusers WHERE username = '".$user."' ");
$row = mysqli_fetch_array($sql);
echo json_encode($row);
exit();
?>
Hope this helps!
Define "POST" method in form tag
download jquery library for offline use or use google cdn or microsoft -then reference it in head html tag
change the data in ajax to data: "user="+user, am used to this format
lastly use proper id names correctly, incorrectly referencing 'ids', you reference in html email id =
$('#jobtitle').val(userData.age);
instead of
$('#email').val(userData.age)
this is fine =>
$('#email').val(userData.email);
$user = $_GET['user']; change to $user = $_POST['user'];
These are some mistake I observed without running you code if still you run into problems ask then
So, I wanna change, using values from a comboBox, another comboBox values (from database MYSQL ). Don't know where the problem is but thinking about that function.
Sorry for my english btw, isn't my native language.
Here are my dropdown lists in HTML:
<select name="Discipline" id="DisciplineList">
<?php include "combo1.php";
?>
</select>
</div>
<div class="alegere2">
<?php echo "Alegeti lectia:" ;
?>
<select name="lectie" id="lectieList">
</select>
Here is the code that I use to fill the second dropdown list using the values from the first one:
< script type="text/javascript">
$(document).on("change","#DisciplineList",function(){
var val = $(this).val();
$.ajax({
url: "combo2.php",
data: {Discipline:val},
type: "GET" ,
dataType: "html",
success: function(result){
$("#lectieList").html(result);
}
});
});
< /script>
Here is the first comboBox fill: ( it fills corectly)
<?php
require("db.php");
$results = mysqli_query($db,"SELECT nume,id FROM discipline");
$nr_discipline=mysqli_num_rows($results);
while($nr_discipline > 0){
$row = mysqli_fetch_row($results);
echo '<option value="'.$row[1].'">'.$row[0].'</option>';
$nr_discipline--;
}
?>
Here is the 2nd comboBox code : (isn't working )
<?php
// Connects to your Database
require("db.php");
$id_discipline = $_GET['Discipline'];
$Query= "SELECT nume,id FROM lectii WHERE id_disciplina =2";
$lectie = mysqli_query($db,$Query);
$nr_lectie = mysql_num_rows($lectie);
while ($nr_lectie > 0) {
$row = mysql_fetch_row($lectie);
echo '<option value="'.$row[1].'">'.$row[0].'</option>';
$nr_lectie--;
}
?>
I don't know where is the problem. It looks like the function is not working at all.
$("#lectie").html(result);
here lectie is class not id replace lectie by lectieList that is the id
In a file following code is written which populate the value of combobox :
<select id="company" required="required" class="form-control" value = "select" name="subject_code" placeholder="Select" >
<?php
//Iterating list of subjects available to be filled .
echo "<option> Select </option>";
foreach ($subjects_to_fill as $subject_id => $subject_name) {
# code...
echo "<option value=".$subject_id."> ".$subject_name." </option>";
}
?>
</select>
On selecting a particular item from the combobox, I want to display the faculty_name dynamically from faculty_table on the basis of $subject_id.
table structure:
faculty_table(faculty_id,faculty_name,subject_id)
subject_table(subject_id,subject_name,faculty_id)
You will need to use ajax for this task.
Add a <div> and below script in your current file.
<div id="faculty"></div>
<script type="text/javascript">
function get_faculty()
{
var id = document.getElementById("company").value;
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_faculty.php",
data: dataString,
success: function(html)
{
$("#faculty").html(html);
}
});
}
</script>
Now create another get_faculty.php file in same folder which will be called on onchange event of company dropdown.
Write below code in that file
if($_POST['id'])
{
$id=$_POST['id'];
$con=mysqli_connect("localhost","username","pass","dbname");
$sql=mysqli_query($con,"SELECT faculty_name from faculty_table where subject_id = ".$id);
while($row=mysqli_fetch_array($sql))
{
echo $row['faculty_name'];
}
}
Dont forget to write mysqli_connect credentials and query accordingly.
Fancybox script POST my form data to go.php page then open go.php in fancybox iframe
<script>
$(document).ready(function() {
$("#fancybox-manual-b").click(function() {
$.post('go.php', $(this).parent().serialize())
.done(function() {
$.fancybox.open({
href : 'go.php',
type : 'iframe',
padding : 5
});
});
});
});
</script>
<select name="country">
<option value="US">US</option>
<option value="EU">EU</option>
</select>
<input type="button" value="Calculate" id="fancybox-manual-b"/>
in go.php, I receive the POST data from the form correctly and when I try to insert this data into DB, it's been inserted correctly too!
But now I want to select * from my DB table to echo all the data in the table where column = POST data, but this query doesn't work!
<?php
$country = $_POST[country];
mysql_query("INSERT INTO calculator (country) VALUES ('$country')"); //Works Correctly
$result = mysql_query("SELECT * FROM calculator WHERE country='$country' ORDER BY ID ASC");
while($row = mysql_fetch_array($result)){
echo $row['ID']." ".$row['country'];
} //Nothing appear
?>
Please checkout this method,
$country = $_POST[country];
if(mysql_query("INSERT INTO calculator (country) VALUES ('$country')"));
{
$result = mysql_query("SELECT * FROM calculator WHERE country='$country' ORDER BY ID ASC");
while($row = mysql_fetch_array($result)){
echo $row['ID']." ".$row['country'];
}
}
I used the same code you shared. But added an additional if loop on mysql_query part. It will make sure your select works after data being inserted.
We have to use jquery ajax to send form data to the php page with POST method then receive back any printed data from the php page in the ajax instead of the console.
We will open the fancybox iframe into ajax success instead of the console.
<script type="text/javascript">
$(document).ready(function() {
$("#fancybox-manual-b").click(function() {
$.ajax({
type: 'POST',
url: 'go.php',
data: $(this).parent().serialize(),
success: function(data){
$.fancybox(data);
},
fail:function(data){console.info(data)}
});
});
});
</script>