Frank Frank - 5 months ago 18
PHP Question

Javascript prompt and submit

i want to get a value from JavaScript prompt and pass it to input for finally submit it

<form id="createdirForm"><input type="text" name="" id="createdir"/><form>
<script type="text/javascript">
function createdir(){
var newdirname;
newdirname = prompt('Please input the directory name:', '');
if (!newdirname) return;
$('createdir').newdirname.value = newdirname;
$('createdirForm').submit();
}
</script>

Answer

The reason why it's not working is because you're not calling the selector in your jQuery properly.

You might want to take a look at the jQuery Selector Tutorial.

Now to fix your problem, since you want to get the input and the form by the id attribute, just like in CSS, you need to use the hashtag (#) symbol.

So you would do:

$('#createdir').val(newdirname);
$('#createdirForm').submit();

Here's a working example: https://jsfiddle.net/vm4ah1L3/1/

Just a side note, you might want to add a name to your html input otherwise when you submit it to your PHP, you won't be able to retrieve the value since the name attribute is empty.

<input type="text" name="dirName" id="createdir">

And in your case, since you're declaring the variable newdirname and assign it right after, you could simply do the assignation in one line:

var newdirname = prompt("Please input a directory name: ");