Log in

View Full Version : dynamic change of an input (text)



sfchun
05-04-2011, 04:23 PM
Hello ,

I'm having an issue when trying to change an input text value dynamically.
here is the picture :)

code :
the page is written in php generating some javascript...

purpose :
dynamically control an input, and following this input changing the state of an other one (that will determinate if the form can be submited).

info :
- UserInputID input is the one to be checked in real time
- validityID input is the one to be changed dynamically
- isShortNameKey(event) keydown, controls the input key and do not allow some specific caracters (it works fine).
- validateUserInput(event) , keyup event management, check the input result and check if already exists in database, then report it in targetDiv (it works fine).

problem :
After comparing the input with db, i would like to change the status of validityID input text.
If input already exists indatabase, validityID should be set to NO, else to YES.

php page

[...]
echo '<input type="text" id="validityID" name="validityNAME" value="NO" />';

echo "<input type=\"text\" size=\"8\" maxlength=\"8\" id=\"UserInputID\" name=\"UserInputName\" value=\"\" style=\"background-color:#ccffbb\" on
keypress=\"return isShortNameKey(event)\" onkeyup=\"validateUserInput(event)\" /></td>";
echo "<td><span id=\"targetDiv\"><i>Machine Type validity</i></span>";
[...]

if ($dbresult == $userinput) {
echo "<script type=\"text/javascript\">
setthisinputboxtextto('validityID','NO');
</script>";
echo '<span style="color:red">not ok, it already exists</span>';
} else {
echo "<script type=\"text/javascript\">
setthisinputboxtextto('validityID','YES');
</script>";
echo '<span style="color:green">ok it does not exists yet</span>';
}

setthisinputboxtextto function (Javascript)

function setthisinputboxtextto(inputboxid, valuetoset) {
if(!document.getElementById ) return; // Make sure browser supports getElementById
var inputObj = document.getElementById(inputboxid);

inputObj.value = valuetoset;

alert('inputboxid : '+inputboxid);
alert('valuetoset : '+valuetoset);
}


So ,
- the (php) test comparing database and input is always working fine
- but no alert comes up later in javascript ... and it never change the text content of validityID input box.


If anyone can see something evident, that i missed :(
Thanks for your help...

sfchun
05-18-2011, 09:00 AM
Hi guys,

Sorry to ask again, but i'm still stucked :(

no one have a clue or work around ?

JoeDaStudd
05-19-2011, 12:16 PM
Look into using Ajax.
It allows you to run php via javascript then use any return values in the javascript.

You have to remember php is server side and javascript is client side.

sfchun
05-27-2011, 01:30 PM
Thanks for your answer, but i'm a bit confused from what you wrote...

Look into using Ajax.??? isn't it what i'm doing ?

It allows you to run php via javascript then use any return values in the javascript.I'm now willing to run javascript through php, not the invert.

You have to remember php is server side and javascript is client side.Did i wrote anything that make you think i missanderstand this ??? :confused::confused:

traq
05-27-2011, 02:23 PM
there is no ajax in the code you posted. "ajax" is javascript using XHR. it sends a request back to your server, in the background, waits for a reply, and then processes it in some way.

I'm sure the reason Joe reminded you that php works on the server is because the snippet you posted:
echo "<input type=\"text\" size=\"8\" maxlength=\"8\" id=\"UserInputID\" name=\"UserInputName\" value=\"\" style=\"background-color:#ccffbb\" on
keypress=\"return isShortNameKey(event)\" onkeyup=\"validateUserInput(event)\" /></td>";
echo "<td><span id=\"targetDiv\"><i>Machine Type validity</i></span>";
[...]

if ($dbresult == $userinput) {
echo "<script type=\"text/javascript\">
setthisinputboxtextto('validityID','NO');
</script>";
echo '<span style="color:red">not ok, it already exists</span>';
}makes it look like you're printing some output, and then trying to wait for user input before printing more output to the page. this is not possible: once php has printed the page, it's done. you need to send a new request to do further processing.

I understand that there may be other functions, that you didn't show above, that include your ajax functions and return values; but we can only offer advice based on what you show us.

If you think you need to clarify anything for us, please do so. :)
otherwise, I'd recommend learning more about AJAX. the jQuery library has very easy-to-use ajax functions (http://api.jquery.com/?ns0=1&s=ajax).

sfchun
06-16-2011, 11:27 PM
sorry for the late reply.

I'll try to figure out my problem with what you told me.
I don't realy have time to learn about Jquery anyway ... i've been asked to avoid third party softwares/libraries.

thanks both reply.