Translate

Monday, May 23, 2022

jQuery - AJAX to change the value in an Input box

I made a form with two input boxes, one with an id of vaue2 and another with an id of value3. The value in the value3 input gets loaded with a value from the database when I tab from value2 to value3. This is the form:

<form action="{{.Page}}" name="form1" id="form1" method="POST" target="iframename" onsubmit="event.stopPropagation(); event.preventDefault();">
<table id="input4Form" style="vertical-align: middle;">
	<tr>
		<td></td><td colspan="4"><b>{{.Title}}</b></td>
	</tr>
	<tr>
		<td style="text-align: right;">{{.Label1}}</td>
		<td><input id="value1" type="text" name="value1" title="{{.In1_Title}}" maxlength="60" onkeydown="if (event.keyCode == 13) document.getElementById('input2search').click()"/></td>
		<td>  </td>
		<td style="text-align: right">{{.Label2}}</td>
		<td><input id="value2" type="text" name="value2" title="{{.In2_Title}}" maxlength="60" onkeydown="if (event.keyCode == 13) document.getElementById('input2search').click()"/></td>
		<td>  </td>
		<td style="text-align: right">{{.Label3}}</td>
		<td><input id="value3" type="text" name="value3" title="{{.In3_Title}}" maxlength="60" onkeydown="if (event.keyCode == 13) document.getElementById('input2search').click()"/></td>
		<td>  </td>
		<td><input type="button" id="input2search" value="   Go   " onclick="event.stopPropagation(); event.preventDefault();loadBySubmit2('form1');"/>
		</td>
	</tr>
</table>
</form>
<script type="text/javascript">
	document.getElementById('value2').addEventListener('keydown', getdefAD);
	document.getElementById('value1').focus();

	function getdefAD(val1){	
		if (val1.keyCode == 9) {
			$.post('erp/getDefaultValue?attr=' + $("input#value2").val() +'&id=' + $("input#value1").val(), function(data) {
			if (data) $("input#value3").val(data);
   		});	
		}
	}
</script>

This is the response handler to return a value from the database:

//Get default value for attribute
func getDefaultValue(w http.ResponseWriter, r *http.Request) {
	sA := "attr"
	sP := r.URL.Query().Get(sA)
	sA = "id"
	sI := r.URL.Query().Get(sA)
	if dataAccess.IsStringInSlice(sP, dataAccess.SlcAD_Default) {
		sR := dataAccess.GetERPADDefault(sP, sI)
		fmt.Fprintf(w, "%s", sR)
	} else {
		//sR = dataAccess.GetERPADDefault(sP, sI)
		//fmt.Fprintf(w, "%s", sR)
		// fmt.Println("Not found")
		return
	}
}

Saturday, May 7, 2022

Golang - Display Active Directory thumbnailPhoto attribute as HTML IMG

I'm working on an application to list all of the active directory attributes that are set for a user. The application uses "encoding/base64" and "github.com/go-ldap/ldap". To display the tumbnai imag, I used this HTML:

"<div style='width:400px;'><img src='data:image/jpg;base64," + b64.StdEncoding.EncodeToString(attrE.ByteValues[0]) + "'/></div>"

This is the code that iterates through all of the returned AD attributes and creates the HTML text:

var sHTML string
var sA []string
for _, entry := range strcAttrs.Entries {
	for _, attr := range entry.Attributes {
		sA = append(sA, attr.Name)
	}
}
sort.Strings(sA)
for _, attE := range sA {
	for _, entry := range strcAttrs.Entries {
		for _, attrE := range entry.Attributes {
			if attrE.Name == attE {
				if ldapAccess.IncSlcStdAttrs(attrE.Name) {
					sHTML = sHTML + "<span style='text-decoration: bold; background-color: green'>"
				} else {
					sHTML = sHTML + "<span style='text-decoration: bold;'>"
				}
				sHTML = sHTML + "attribute: " + attrE.Name + "</span><br />"
				var attr string = ""
				for _, attr = range attrE.Values {
					if ldapAccess.Include(attrE.Name) {
						if ldapAccess.IsDate64(attrE.Name) {
							sHTML = sHTML + "Date: " + ldapAccess.ConvertStringToDate64(attr)
						} else {
							if ldapAccess.IsDateZ(attrE.Name) {
								sHTML = sHTML + "Date: " + ldapAccess.ConvertStringToDateZ(attr)
							} else {
								sHTML = sHTML + attr
							}
						}
					} else {
						if (attrE.Name) == "thumbnailPhoto" {
							sHTML = sHTML + "<div style='width:400px;'><img src='data:image/jpg;base64," + b64.StdEncoding.EncodeToString(attrE.ByteValues[0]) + "'/></div>"
						} else {

							sHTML = sHTML + "Not Displayed"
						}
					}
					sHTML = sHTML + "<br/>"
				}
			}
		}
	}
	sHTML = sHTML + "<br/>"
}