converting javascript to php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gubbachchi
    New Member
    • Jan 2008
    • 59

    #1

    converting javascript to php

    Hi,

    How to convert a javascript variable to php variable. Could anybody help me to do this.
  • harshmaul
    Recognized Expert Contributor
    • Jul 2007
    • 490

    #2
    Hi,
    Javascript runs on the client, and not on the server which is where the php runs.
    You will have to construct a Query String to pass the value over.

    if you tell me when you want to pass the javascript to the php i'll try and give you a solution.

    Comment

    • gubbachchi
      New Member
      • Jan 2008
      • 59

      #3
      Thank u.

      Actually I need to get the data from the html text field to php variable in the same page, so I was using this technique 'calling a javascript function when submit button is pressed and trasnferring the html text field value into the javascript variable and then I thought of converting javascript variable into php variable.

      My requirement is, I have a textbox when user enters some value into that and press submit button, I have to fetch data from mysql based on the value in text field and display it. I want to write both html and php code in the same page. How will I do it. Here is the code

      <form name="Calender1 ">


      <a onmouseover="wi ndow.status='Da te Picker';return true;" onmouseout="win dow.status='';r eturn true;" href="javascrip t:show_calendar ('Calender1.sel ect')">

      <img height=18 src="calendar.g if" width=25 border=0></a>
      <input class="textBox" type="text" readonly name="select" id="select" size="7">
      <input type="submit" id="BtnSubmit" name="BtnSubmit " value="search" onclick= "senddate() ">

      </form> <br><br><br>


      <?php
      require_once("D BConnect.php");

      $sql="SELECT * FROM User_Calender WHERE user_id='2' and cur_date='2008-02-05'";
      $result = mysql_query($sq l);
      $num = mysql_num_rows( $result);
      ?>

      Comment

      • harshmaul
        Recognized Expert Contributor
        • Jul 2007
        • 490

        #4
        Hi,
        Heres my solution...

        Code:
        <?php
        if(IsSet($_POST['select'])){
        echo $_POST['select'];
        }
        ?>
        
        <form action="phpcode.php" method="post"  name="Calender1">
        <a onmouseover="window.status='Date Picker';return true;" onmouseout="window.status='';return true;" href="javascript:show_calendar('Calender1.select')">
        <img height=18 src="calendar.gif" width=25 border=0></a>
        <input class="textBox" type="text" readonly name="select" id="select" size="7">
        <input type="submit" id="BtnSubmit" name="BtnSubmit" value="search" onclick= "senddate()">
        </form>

        Comment

        • Bodyloss
          New Member
          • Feb 2008
          • 6

          #5
          Hey
          harshmaul's answer is compleatly correct, but if your planning on submitting this data to a database, ensure you validate the input first, so as to avoid sql injection attacks
          You can simply check that the post variable passes ctype_alnum to make sure that its made only of charects and numbers, but if the input needs to have erronious chararects (correct me if this is the wrong word) then you could use addslashes() on the input
          [PHP]
          if (isset($_POST['input'])) {
          //you can now addslashes or check it passes ctype_alnum
          //so this
          if (ctype_alnum($_ POST['input'])) {
          $variable = $_POST['input'];
          }
          //or you can do this
          $variable = addslashes($_PO ST['input']);
          }
          [/PHP]

          Good luck

          Comment

          • harshmaul
            Recognized Expert Contributor
            • Jul 2007
            • 490

            #6
            Good point bodyloss. Watch out for the sql injection attacks.

            Comment

            • gubbachchi
              New Member
              • Jan 2008
              • 59

              #7
              Thank you so much for your solutions

              Comment

              Working...