homepage Welcome to WebmasterWorld Guest from 23.23.8.131
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Using alert/prompt field in postvars
kajje



 
Msg#: 4206232 posted 7:03 am on Sep 24, 2010 (gmt 0)

Scope:
My php code generates a list of files, the links send a link to php with the request to rename the file.

HTML OUTPUT

<html>
<head>
<script LANGUAGE="JavaScript">
<!--
function alertboxRename(FileNameOld)
{
var agree=confirm('Are you sure you want to rename the file "+FileNameOld+' ?');
if (agree)
return true ;
else
return false ;
}
// -->
</script>
</head>
<body>
filename1.txt <a href=?action=rename&file=filename1 onClick="return alertboxRename('filename1.txt')">rename</a>
filename2.txt <a href=?action=rename&file=filename2 onClick="return alertboxRename('filename2.txt')">rename</a>
filename3.txt <a href=?action=rename&file=filename3 onClick="return alertboxRename('filename3.txt')">rename</a>
</body>



What I would like is to have a prompt box instead of an alert box.
The prompt box shows the original filename and asks a new filename. All variables are then sent back to the script so I can catch them up in php.

Additionally it would be nice to have the original filename as value
in the promptbox for easy editing.

+--------------------------------+
| old filename: filename1.txt |
| |
| new filename: |
| +-------------------+ |
| | filename1x.txt | |
| +-------------------+ |
| |
| +----+ +--------+ |
| | OK | | CANCEL | |
| +----+ +--------+ |
+--------------------------------+


OK sends out the request "?action=rename&filenameold=filename1.txt&filenamenew=filename1x.txt"
CANCEL does nothing

Is a REQUEST the way to go, or is this more easy to implement using POST within a form? I could catch everything up in php which would load an additional form in a new page but I rather do it in javascript.

Any ideas are welcome.

 

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4206232 posted 1:48 pm on Sep 24, 2010 (gmt 0)

Is a REQUEST the way to go, or is this more easy to implement using POST within a form?

By "REQUEST" I think you mean a GET request. I would advise against using a GET request to do this:

[w3.org...]

The "get" method should be used when the form is idempotent (i.e., causes no side-effects). Many database searches have no visible side-effects and make ideal applications for the "get" method.

If the service associated with the processing of a form causes side effects (for example, if the form modifies a database or subscription to a service), the "post" method should be used.


In your example, the processing of this data would cause a side effect (the file will be renamed), so a POST should be used.

Now, as for how to achieve the UI, you won't be able to rely on the built in alert/confirm methods if you want to display a dialog that contains an input for the new filename. You may want to check out the Yahoo UI Library (YUI) for accomplishing this. With YUI3, you'd be looking at using Overlay (http://developer.yahoo.com/yui/3/overlay/), and with YUI2 you'd be looking at one of the Container components (http://developer.yahoo.com/yui/container/), probably Dialog which has an example that you might be able to base yours off of (http://developer.yahoo.com/yui/examples/container/dialog-quickstart.html).

Side note:

<script LANGUAGE="JavaScript">
<!--
...
// -->
</script>

1. The LANGUAGE attribute is invalid. Use the type attribute instead.
2. Don't include HTML comments inside script elements. This hasn't been needed for 15 years or so.
So your code should look more like this:

<script type="text/javascript">
...
</script>

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4206232 posted 1:58 pm on Sep 24, 2010 (gmt 0)

Also, I would start by figuring out how this would work if you didn't have JavaScript, and then use JavaScript to enhance the experience. For example, maybe you have the form for renaming the filename in the page already:


<form action="" method="post">
<input type="hidden" name="action" value="rename">
<input type="hidden" name="filenameold" value="filename1">
<input name="filenamenew" value="filename1.txt">
<input type="submit" value="Rename">
</form>
<form action="" method="post">
<input type="hidden" name="action" value="rename">
<input type="hidden" name="filenameold" value="filename2">
<input name="filenamenew" value="filename2.txt">
<input type="submit" value="Rename">
</form>


Then you could use JavaScript to hide the input element and submit button or change the way it gets displayed.

Hope that helps.

kajje



 
Msg#: 4206232 posted 2:01 am on Sep 26, 2010 (gmt 0)

Can I get the value of the prompt-box projected in my form?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved