...

View Full Version : help with sending data via showprompt function



clem25
03-27-2012, 04:07 PM
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();

Mishu
03-28-2012, 05:24 AM
A few points:

1. This line has a typo in it


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


<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>
<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.
$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum