Click to See Complete Forum and Search --> : [RESOLVED] Posting a form from select options problem


barneybarney68
07-21-2009, 10:26 AM
Hey, if this is not cinsidered the right place to post then please a mod can delete/lock etc...

I'm connecting to my database and reading the result from a tatable, i display the results along with a form that comprising of a dropdown option list. On selection of one of the items i want the form to be posting to the current page to be handeled. However before posting commenced i want to make sure the user means what that are doing e.g. delting row from db. Here is some of the script:

<<<HTMLBLOCK

<tr>
<td>data 1</td>
<td>data 2</td>
<td>data 3</td>
<td>data 4</td>
<td>data 5</td>
<td>data 6</td>
<td>data 7</td>
<td>data 8</td>
<td>
<form action="/subdirectory/currentpage.php" method="post">
<input type="hidden" name="id" value="id#" />
<select name="change_status" onChange="this.form.submit()">
<option value="for sale" onclick="return confirm('Are you sure you want to change to for sale?')">For Sale</option>
<option value="sold" onclick="return confirm('Are you sure you want to change to sold?')">Sold</option>
<option value="update" onclick="return confirm('Are you sure you want to update?')">Update</option>
<option value="delete" onclick="return confirm('Are you sure you want to delete?')">Delete</option>
</select>
</form>
</td>
</tr>

HTMLBLOCK;


The problem is that the onchange submits the form before the conform dialog acts, i.e by the time the conform box appears the new page is loaded and selecting yes or no hence does nothing.

How can i make it so that i select an action then confirm/or not then the script posts/or not.

Thanks
p.s. i would like to keep the action in the dialogue box so the user knows what they are agreeing to, e.g. do you want to delete.

johanafm
07-21-2009, 10:31 AM
Edit: Ah, a bit too hasty.


An easy way to do what you want is to remove the onclick events from the radios, then
[html]
<select name="change_status" onchange="submitIfConfirmed(this);">


And the javascript

function submitIfConfirmed(sel) {
switch(sel.selectedIndex) {
case 0:
var conf = confirm('Are you sure you want to change to for sale?')
break;
case 1:
var conf = confirm('Are you sure you want to change to sold?');
break;
case 2:
var conf = confirm('Are you sure you want to update?');
break;
case 3:
var conf = confirm('Are you sure you want to delete?');
break;
default:
var conf = false;
}
if (conf)
document.forms[0].submit();
}

barneybarney68
07-24-2009, 04:02 PM
I'll give this a try, since i dont know java but it looks simple and it makes sense. Thanks.

barneybarney68
07-24-2009, 04:25 PM
sorry this code stop my script from running at all, I've tracked it down to the second part of code. Any thoughts

paulnaj
07-24-2009, 04:33 PM
Hi,

Just checking - where did you put the javascript code. (By the way - it's javascript, not java - amazingly they're unrelated!!)

P

barneybarney68
07-28-2009, 03:25 PM
I put the javascript at the top of my php script then the html code in the form section in my php script which is in here now tags

barneybarney68
07-28-2009, 04:05 PM
ok this now works fine i echoed the script and it worked now. However because i have the form in a php while loop the form changes on every database row listed and this the

document.forms[0].submit();

won't will work only for the first from created. even if i give them an id the while copied the same id tage and the first one will get submitted. Any ideas on this? Thanks

paulnaj
07-28-2009, 04:59 PM
In your function change ...

document.forms[0].submit();

... to ...

sel.form.submit();

(That is, submit the 'parent' form of the select box)

P

barneybarney68
07-28-2009, 05:18 PM
Thank you it works like a charm. ;-)