title: Setting value to textbox using PHP with help of Javascript
body: <p>What I intend to do is a dialog box will prompt and the user will input his username, when clicked OK, the php codes inside the javascript function mySearch will get his details in mysql and display it in the textbox. HELP. T_T It won't work.</p>
  
  <pre><code>    &lt;script&gt;
          function mySearch()
          {    var name = prompt("Search","Enter username");
  
              if (name!=null &amp;&amp; name!="")
              {
                      var sentval = document.getElementById("sentval");
                      sentval.value = name;
  
                      &lt;?php
                          $myLink = mysql_connect('localhost','root', '1234') or die(mysql_error());
                          $selectDB = mysql_select_db('proj2db', $myLink) or die(mysql_error());
                          $sql = "SELECT * FROM userTBL WHERE uName ='".$_POST['sentval']."'";
                          $exec = mysql_query($sql, $myLink);
                          $row = mysql_fetch_array($exec);
  
                          $uname = $row['uName'];
                          $pwd = $row['pwd'];
                          $code = $row['sAns'];
                          $link = $row['path'];
                      ?&gt;              
              }
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form method="post" action="" name="myform" id="myform"&gt;
          &lt;input type="hidden" id="sentval"/&gt;
          &lt;center&gt;
              &lt;table&gt;
                  &lt;tr&gt;    
                      &lt;td&gt;&lt;font face="Tahoma" size = "2" color="#0B3861"&gt;Username:&amp;nbsp&amp;nbsp&lt;/font&gt;&lt;/td&gt;
                      &lt;td&gt;
                          &lt;input type = "text" id = "uname" name = "uname" size="35" value="&lt;?php echo $uname ?&gt;"/&gt;
                          &lt;button type="button" style="height: 25px; width: 60px" onclick="mySearch()"&gt;Search&lt;/button&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;    
                      &lt;td&gt;&lt;font face="Tahoma" size = "2" color="#0B3861"&gt;Password:&amp;nbsp&amp;nbsp&lt;/font&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type = "password" id = "pwd" name = "pwd" size="35" value="&lt;?php echo $pwd ?&gt;"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;font face="Tahoma" size = "2" color="#0B3861"&gt;Re-type Password:&amp;nbsp&amp;nbsp&lt;/font&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type = "password" id = "repwd" name = "repwd" size="35" value="&lt;?php echo $pwd ?&gt;"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;font face="Tahoma" size = "2" color="#0B3861"&gt;Code:&amp;nbsp&amp;nbsp&lt;/font&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type = "text" id = "code" name = "code" size="35" value="&lt;?php echo $code ?&gt;"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;font face="Tahoma" size = "2" color="#0B3861"&gt;Link:&amp;nbsp&amp;nbsp&lt;/font&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type = "text" id = "link" name = "link" size="35" value="&lt;?php echo $link ?&gt;"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;                        
                      &lt;td colspan=2 align="right"&gt;
                          &lt;font face="Tahoma" size = "2" color="#0B3861"&gt;(e.g. http://www.google.com or index.php)&lt;/font&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;                        
                      &lt;td colspan=2 align="right"&gt;
                          &lt;input type="submit" name="submit" value="Save" style="height: 25px; width: 60px"/&gt;
                          &lt;button type="button" style="height: 25px; width: 60px" onclick="location.href='adminMain.php'"&gt;Cancel&lt;/button&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;               
              &lt;/table&gt;
          &lt;/center&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;?php
      if (!empty($_POST['uname']) &amp;&amp; !empty($_POST['pwd']) &amp;&amp; !empty($_POST['repwd']) &amp;&amp; !empty($_POST['code']) &amp;&amp; !empty($_POST['link']))
      {
          if ($_POST['pwd'] == $_POST['repwd'])
          {
              $myLink = mysql_connect('localhost','root', '1234') or die(mysql_error());
              $selectDB = mysql_select_db('proj2db', $myLink) or die(mysql_error());
  
              $sql = "UPDATE `proj2db`.`usertbl` SET pwd='".$_POST['pwd']."', sAns='".$_POST['code']."', path='".$_POST['link']."' WHERE uName='".$_POST['uname']."';";
              $exec = mysql_query($sql, $myLink);
          }
          else
              echo "&lt;font face='Tahoma' size = '2' color='red'&gt;&lt;center&gt;Error: Password mismatched &lt;br&gt; Press cancel to return to home page &lt;/center&gt;&lt;/font&gt;";
      }
  ?&gt;
  </code></pre>
  
  <p></p>