开发者

Can I do this with PHP?

Consider,

  <html>
      <head>
          <title>txt with js eff</title>
      </head>

      <body>
          <script type = "text/javascript">
              function transfer(which) {
                  document.getElementById("temp_name").value = which;
              }
          </script>

          <form action="" method="post" name="frm1">
              <label> In put 1 </label>
              <input
                type="text"
                name="username"
                id = "username"
                onkeyup = "transfer(this.value)"><br/><br/>

              <label> In put 2 </label>
              <input
                type="text"
                name="temp_name"
                id = "temp_name">
          </form>
      </body>
  </html>

I need to do this by using PHP:

I need to pass the value to "In put 2" from "In put 1" when the user focuses his/her cursor to next field. I can do it ve开发者_开发问答ry easily with JavaScript, but I need to this with PHP.

Is there a solution?


You don't do this type of stuff with PHP, you do it with Javascript. PHP only runs on the server. You would have to post this page back to the server, have PHP write a block of javascript that causes a particular field to be focused once the page is loaded, then spit this back out for the user. In short, even if you worked out a way to do it with PHP, you'd end up doing it with Javascript still.


PHP is a server side language so it's job is just to generate the document that your browser will render. So once that document is created, there's no changing it. It's up to client side languages such as javascript to do so. The mouseover you are describing is definitely a job for something client-side.

You can request a page from the server with the new value but I don't think that's what you want to do.


Jonathan is right, you don't do that in PHP.

However, you have your code in Quotes, which leads me to believe that this code is in a string and you are presenting it to the user, in which case, there wouldn't be a value in either of the input boxes at execution time. What is the applicaion?


Maybe you want to save the values. Just call the submit method after you make the changes in JavaScript, and then save it in the database or in session or whatever. Or maybe you can make an Ajax request to avoid the page reload, but really it's hard to know what you are trying to achieve or understand.


The user is either "doing" something or the user is "asking" for something.

If the user is just "doing" something, then it is all happening in the browser, like reading, typing and so on. The user asks for something by clicking a link or pressing a button. The program that responds to the user asking for something is written in PHP and PHP runs on the server. The program that responds to the user doing something is written in JavaScript and runs in the browser.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜