Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help with sending data via showprompt function

    I m trying to send data to my database (Mysql). When I enter things in the prompt box it doesn t go to my database.

    Can someone tell me why, please?

    Here is my code:

    <script type="text/javascript">
    function show_prompt()
    {
    var name=prompt("question");
    if (name!=null && name!="")
    {
    //set the hidden input value to the value entered in the prompt
    document.input.purpose.value = name; //document.input referring to the form named 'input'
    document.input.submit(); //submit the form
    }
    }
    </script>




    <form id="propose" name="input" action="insertpropose.php" method="post"><br/>
    <input type="submit" onclick="show_prompt()" value="propose" />
    <input type="hidden" name="propose" value="">
    </form>


    and the insertpropose.php form is:

    $propose=$_POST['propose'];


    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");

    $query = "INSERT INTO propose VALUES ('','$propose')";
    mysql_query($query);

    mysql_close();

  • #2
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    A few points:

    1. This line has a typo in it

    Code:
    document.input.purpose.value = name;
    2. Instead of a hidden input, why not make the input a textbox with type="text" and then you won't need the window.prompt at all unless this is a homework requirement.

    3. It's not normally a good idea to put an onclick on a submit button.
    A better way is to add an onsubmit event handler to the form which is then run when the submit button is clicked. The onsubmit function then needs to return tru or false to the onsubmit event which then either submits the form or cancels the form submission

    Code:
    <form id="propose" name="input" action="insertpropose.php" method="post" onsubmit="return show_prompt()">
        <input type="submit"  value="propose" />
        <input type="hidden" name="propose" value="">
    </form>
    Code:
    <script type="text/javascript">
    function show_prompt()
    {
    var name=prompt("question");
    if (name!=null && name!="")
      {
      //set the hidden input value to the value entered in the prompt
      document.input.purpose.value = name; //document.input referring to the form named 'input'
      return true;; //submit the form
      }
    return false; //don't submit the form
    }
    </script>
    4. it's also not normally a good idea to have names of elements the same as element types. I would change the name of the form from input (which is an element type) to something else..ie..frmInput

    5.
    PHP Code:
    $query "INSERT INTO propose VALUES ('','$propose')"
    This query is implying that your table propose has only 2 columns in it and nothing else. If that is not the case you need to specify the column name after the table name in your $query.
    Last edited by Mishu; 03-28-2012 at 05:33 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •