Currently working on user management page where there are name , username , email and level fields . All fields except for level are obtained from AD. I managed to make the values saved into database. However , the name field takes both username and email field instead. The dropdown would display the name but it saves username and email.
<th align="right" scope="row">Select User</th>
<td><script type="text/javascript">
var $ = function(e){ return document.getElementById(e); }
var swap = function(val){
var arr = val.split("|");
document.getElementsByName('username')[0].value = arr[0];
document.getElementsByName('email')[0].value = arr[1];
}
</script>
<select name="name" id="name" class="reginput" onchange="swap(this.value)" />
<option value="">Select User</option>
<?php
// Create connection
$con=mysqli_connect("localhost","root","","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
//$result = mysqli_query($con, "SELECT name,Id FROM users");
$result = $this->Users_model->queryAD();
$options=$test;
//while ($row=mysqli_fetch_array($result)) {
for ($i=0; $i<$result["count"]; $i++) {
//$options .= '<option value="'.$row["name"].'">'.$row["name"].' '.'</option>';
$options .= '<option value="'.$result[$i]["samaccountname"] [0].'|'.$result[$i]["mail"][0].'">'.$result[$i]["displayname"][0].' '.'</option>';
}
$options .= '</select>';
echo $options;
For example :
Select User : Jane Doe
Username : Jane
Email : janed#yahoo.com
Level : Admin
User would be displayed as Jane | janed#yahoo.com instead of Jane Doe but other fields are fine.
This is the page for inserting users , the display is fine but Full Name wont be saved properly into DB
It takes the username and email instead of Full name because the value of the option you are giving is $result[$i]["samaccountname"][0].'|'.$result[$i]["mail"][0]
Change the value of the option to
$options .= '<option value="'.$result[$i]["displayname"][0].'">'.$result[$i]["displayname"][0].' '.'</option>';
EDIT
To display username and email to user, and saving Fullname to DB, use this:
$options .= '<option value="'.$result[$i]["displayname"][0].'">"'.$result[$i]["samaccountname"] [0].'|'.$result[$i]["mail"][0].'"</option>';
Related
I have run into a problem and Im not sure where to start... So my problem is- I need to use a dropdown which is automatically populated by a query concatenating two fields to make a single full-name. Then based off the name of the person selected change the value of the text input to be the dob, then also update every change when a new name selected. I have tried using the onchange event for select but just cant work out the right combination.
<?php
$conn = new mysqli('localhost', 'username', 'password', 'database') or die ('Cannot connect to db');
$result = $conn->query("SELECT id, dob, CONCAT(`space`,`firstname`,`lastname`) AS `whole_name` FROM `table`");
echo "<select name='id'>";
while ($row = $result->fetch_assoc()) {
$data = $id
unset($id, $name);
$id = $row['id'];
$name = $row['whole_name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
?>
<input type="text" id="text" value="<?php echo "$data" ?>"/>
Things to note - due to the concatenate needing a separator 'space' has been added to a field. at this time the sql query works exactly as intended. When loading the page in the current state the text field comes up with the correct value for the first name, but does not update when changing value of select.
Im new with PHP and mySQL - self learning as I go. Any help appreciated.
Ajax not needed here. You can just put the dob value as special attribute for each options, and then change the text input with javascript when the select value change.
Here the new select with dob in attribute :
while ($row = $result->fetch_assoc()) {
$data = $id
unset($id, $name);
$id = $row['id'];
$dob = $row['dob'];
$name = $row['whole_name'];
echo '<option dob="'.$dob.'" value="'.$id.'">'.$name.'</option>';
}
and then update the input when the select change, here an example with jquery :
$("select[name=id]").change(function(event) {
//get the dob attribute
var dob = $('option:selected', this).attr('dob');
// update the text input
$("input#text").val(dob);
});
I have two dropdowns in my form.
1)Brand Name of car
2)Model name in which data is going to be retrieved from database on the basis of brand name selected.
so I have taken a div tag and appended that second dropdown.The data in dropdown is displayed properly but the problem is in storing the data selected from the second dropdown, It stores the model name upto the space only.length in the database for that field is also taken properly.
eg- if the model name is "series 1" than it will store only series.
Php page of second dropdown:
<?php
$brand = $_GET['brand'];
include('connection.php');
$sql = "select * from modelname where Brand_Name='" . $brand . "'";
$result = mysqli_query($conn, $sql);
$html = "";
$html = '<select name="model">';
while ($row = mysqli_fetch_assoc($result)) {
$html.='<option value=' . $row['M_Name'] . '>' . $row['M_Name'] . '</option>';
}
$html.='</select>';
echo $html;
?>
because you are not quoting your attributes.
value=hello world
is seen as
value="hello" world
Add the missing quotes.
Sorry for the obtuse title not quite sure how to describe this one. I have options that are dynamically created through a call to a database with php. The dropdown list options are set like this:
<div class="input-group col-md-12"><span class="input-group-addon">Tag Source</span>
<select class="form-control" name="tagtype" value="<?php echo addslashes($_POST['tagtype']); ?>">
<option value="">Tag Source</option>
<?php
foreach ($sources as $row) {
?>
<option value="'".<?php $row['sources']; ?>."'"><?php echo $row['sources']; ?></option>
<?php
}
?>
When I update the database I thought it would update the value to what I have set it as with php:
<option value="'".<?php $row['sources']; ?>."'">
But instead it does not update the database properly. My guess is that I have to write a javascript function to set the value to post to the db but would welcome any instruction!
EDIT: This is how I update the database
$conn = new mysqli(intentionally left blank);
include('login.php');
if($_POST['submit']) {
if ($_POST['tagname']=="") $error.="<br />Please enter a tag name!";
if ($_POST['tagtype']=="") $error.="<br />Please enter a tag type!";
if ($_POST['url']=="") $error.="<br />Please enter a tag URL!";
if ($_POST['publisher']=="") $error.="<br />Please enter a publisher!";
if ($_POST['advertiser']=="") $error.="<br />Please enter an advertiser!";
if ($_POST['identifier']=="") $error.="<br />Please enter an ID!";
if ($_POST['ecpm']=="") $error.="<br />Please enter the eCPM rate!";
if ($_POST['ccpm']=="") $error.="<br />Please enter the eCPM rate!";
if ($_POST['datebrokered']=="") $error.="<br />Please enter the date brokered!";
else {
if (mysqli_connect_error()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$identifier = $_POST['identifier'];
$sql = "SELECT unique_id FROM jpctags WHERE identifier=?";
$stmt = $conn -> prepare($sql);
$stmt -> bind_param('s',$identifier);
$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($uniqueid);
$stmt -> fetch();
if ($uniqueid) $error = "This tag already exits within the system, please edit the tag instead.";
else {
$tagname = $_POST['tagname'];
$tagtype = $_POST['tagtype'];
$identifier = $_POST['identifier'];
$url = $_POST['url'];
$publisher = $_POST['publisher'];
$advertiser = $_POST['advertiser'];
$ecpm = $_POST['ecpm'];
$ccpm = $_POST['ccpm'];
$datebrokered = $_POST['datebrokered'];
$sql = "INSERT INTO jpctags (`tagname`, `tagtype`, `identifier`, `url`, `publisher`, `advertiser`, `ecpm`, `ccpm`, `datebrokered`, `user_id`) VALUES(?,?,?,?,?,?,?,?,?,?)";
$stmt = $conn -> prepare($sql);
$stmt -> bind_param('ssssssiisi',$tagname, $tagtype, $identifier, $url, $publisher, $advertiser, $ecpm, $ccpm, $datebrokered, $user_id);
$stmt -> execute();
}
}
}
You are just returning the row from your tables as values to your option. You should actually echo them:
<option value="<?php echo $row['sources']; ?>">
You need the form to POST to a php script to update the db.
Check out php form handling here: http://www.w3schools.com/php/php_forms.asp
Make sure to handle the input properly (i.e. escape the input with http://php.net/manual/en/mysqli.real-escape-string.php) because a user could edit the <select> dropdown values and execute a SQL injection.
How to get my AJAX results from the input of a textbox populated from mysql live on the page.
I already got it to work with the use of textboxes, so if you check them then the page gets updated with the results of mysql.
The same I would like to use for my textboxes as soon as you type something in or select something from the autocomplete list (this would be even beter).
I have the following code that I want to use for the textbox:
HTML:
<input type="text" name="naam_klant" size="20" id="naam_klant" onkeyup="lookup(this.value);" onblur="fill();" >
<div class="suggestionsBox" id="suggestions" style="display: none;">
<div class="suggestionList" id="autoSuggestionsList">
</div>
</div>
PHP:
$pdo = new PDO('mysql:host=localhost;dbname=records', 'root', '***');
$select = 'SELECT *';
$from = ' FROM overboekingen';
$where = ' WHERE FALSE';
$opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
if (in_array("naam_klant", $opts)){
$where .= " OR naam_klant = [$queryString%] ";
}
$sql = $select . $from . $where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
I am working on a very basic administrator functionality of a social network and I came across this issue of not being able to remove an option from select dropdown list that I previously generated using jquery. The dropdown list contains all users of the social network. Administrator upon clicking on "Delete account" deletes the corresponding record from the database.
Now the question being - when I click on "delete account" it works perfectly fine but the option with a username is still there in a dropdown list and is possible to be picked - when picked it obviously returns dozens of PHP warnings and errors because the record is not in a database anymore. How can I remove this option straight away? I tried something like the following, but it doesn't work.
admin_panel.php (only relevant stuff)
<select name='users' id='users'>
<option value="" disabled selected>Select user</option>
<?php
$sql = mysql_query("SELECT * FROM users WHERE id <>'".$_SESSION['user_id']."'ORDER BY username DESC") or die(mysql_error());
$userList = [];
while($row=mysql_fetch_assoc($sql)){
$username = $row['username'];
$userID = $row['id'];
$userList .= '<option name="userID" value='.$userID.'>'.$username.'</option>';
}
echo $userList;
?>
</select></br></br></div>
<div id="user_info">
<!-- generated user info table-->
</div>
<script type="text/javascript">
"$('#user_info').on('click', '#deleteAccount', function(e){
data.command = 'deleteAccount'
data.userID = $('#users').val()
$.post(theURL, data, function(result){
//Do what you want with the response.
$('#delete_account_success').html(result);
})
$("#users option[value='data.userID']").remove();
$('#delete_account_success').show();
$('#delete_account_success').fadeOut(5000);
})
</script>
processUser.php (part of a switch statement)
if(isset($_POST['command'])){
$cmd = $_POST['command'];
$userID = $_POST['userID'];
$sql=mysql_query("SELECT * FROM users WHERE id='".$userID."'");
$userData = [];
while($row = mysql_fetch_assoc($sql)){
$userData['userid'] = $row['id'];
$userData['username'] = $row['username'];
$userData['name'] = $row['name'];
$userData['date'] = $row['date'];
$userData['email'] = $row['email'];
$userData['avatar'] = $row['avatar'];
$userData['about'] = $row['about'];
$userData['admin'] = $row['admin'];
}
switch($cmd){
case 'deleteAccount':
$sql= "DELETE FROM users WHERE id =".$userID;
$result=mysql_query($sql);
echo "<img src='pics/ok.png' class='admin_updated_ok'>";
break;
}
On this line
$("#users option[value='data.userID']").remove();
You're removing any option items from #users where the value is equal to the string literal data.userID
Try changing it to
$("#users option[value='" + data.userID + "']").remove();