Link to home
Create AccountLog in
Avatar of abangbatax

asked on

parse inputbox onkeypress to php string


can anyone help me code this? ( sorry for bad english if any )

example :

all the codes are in a php file ( ex. form.php )
i have an inputbox in html form ( <input type="text" id="prefix" name="prefix" value=""> )
then i have php string ( $prefix )
how to set the $prefix value from the inputbox onkeypress event?

do u get what i mean?

Avatar of Steve Bink
Steve Bink
Flag of United States of America image

When the onkeypress event runs, PHP has already stopped running.  Javascript is not aware of PHP, and vice versa.  In order to pass information from javascript to PHP, you'll have to:

a) generate a request (POST/GET?) to another PHP script, or
b) use AJAX to hit a script on the back-end
Avatar of abangbatax


but javascript can read php string value also can execute php code
why it cant do reverse action? pass the javascript value to php string?

can you give me sample for your 'b' answer?
i am currently blind about ajax
i code like this

<input type="text" id="prefix" name="prefix" onkeypress="'<?$fldprefix?>' = document.getElementById('prefix').value; return false">

but value not send to $fldprefix
Javascript cannot read PHP values, and cannot execute PHP code.  PHP can affect how the javascript is built, but only within the PHP layer.  There is no direct interaction between javascript and PHP.  PHP runs on the server, and can create an HTML response to a client.  That response can include dynamically built javascript code, but that code will be static once the client receives it.  When the client receives the response, PHP has finished and exited - the response is the product of PHP's work.  Javascript runs on the client side.  In order for javascript to pass information back to PHP, that information must go to the server.  The only methods a client can use to send information back to the server are POST, GET, and AJAX (which simulates an HTTP GET within the HTML layer).

I am not familiar with AJAX implementations, but there are plenty of experts here that are.  If you'd like to see how to use AJAX, wait for one of them to respond.  The other methods would necessarily require you to submit the form, which I think would be inappropriate for your needs.
To use your code example, the SERVER is aware of $fldprefix.  Let's say $fldprefix='stuff'.  PHP is aware of $fldprefix, and knows it equals 'stuff'.  When PHP reads that line in the file, it creates this line to be sent to the client:

<input type="text" id="prefix" name="prefix" onkeypress="'stuff' = document.getElementById('prefix').value; return false">

The client knows nothing about $fldprefix.  The client does not even know that 'stuff' used to be a variable.  All it knows is the line it received.
what i mean by execute is this sir... :

require ("somefunction.php");
in the <body> i put this

<a href="#" onclick="document.getElementById('sometextarea').value = '<? $doutput = get_data($name, $phone, $address); echo output($doutput); ?>'; return false">show</a>]

after i click 'show'
in textarea value, it shows all the data i need as the $name, $phone, $address are sets
The easiest way is to trigger a form submit after the event however you do not want to use onkeypress because it is fired before the value of the text field has been set; onkeyup would be a better choice, below is a simple example how to accomplish the objective:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
    <title>Untitled Page</title>
<body onload="document.getElementById('prefix').focus();">
    if(isset($_POST['prefix'])) {
        $prefix = $_POST['prefix'];
        echo 'Prefix is '.$prefix.'<br />';
    else {
        $prefix = "";
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="frmInput">
<input type="text" id="prefix" name="prefix" value="<?php echo $prefix; ?>" onkeyup="document.getElementById('frmInput').submit();" />

Open in new window

LordOfPorts, is good
but i got 3 inputbox
how to code?
should i put onkeyup
on every inputbox?
is it possible form in form?

i mean the code above are in another form

like this... example

<form method=POST action="" name="memdata" id="memdata">
  <table width="675" border="0" cellspacing="0" cellpadding="5"><tr><td></td></tr>
		<td><font face="verdana, arial, helvetica" size="2">name</font></td>
		<td><<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="frmInput">
<input type="text" id="prefix" name="prefix" value="<?php echo $prefix; ?>" onkeyup="document.getElementById('frmInput').submit();" />

Open in new window

Avatar of LordOfPorts
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account

i got a problem here. that code its submit to other form too, how to make it not be like that?
In the standard submit the entire page is submitted so all forms are affected. You could alter the other forms of the page to remember the entered values however if that is not the approach you want to take you could look into using AJAX as per routinet's initial suggestion. Through AJAX you can trigger background actions based on events such as onkeyup that would not submit the form but instead retrieve values from a separate page seemlessly in the background, you then accept the returned values through Javascript, parse them if they are in XML or JSON format, and use them in the form. It is however considerably more complex compared to the simple form submit above.