mikey186 mikey186 - 10 days ago 6
HTML Question

Change form action URL using JavaScript from a HTML value

I was making a uploader on Electron. As unfortunately, the framework doesent work with PHP, so JavaScript is the only way to function.

I made a diagram here

I tried using that code but didn't help.

<script type="text/javascript">
function OnSubmitForm()
{
if(document.pressed == 'submit')
{
document.stash.action = "document.getElementByID("")[0].value";
}
return true;
}
</script>


and used this HTML Code

<form name="stash" action="Action URL">

Answer

Detect input field change event

either with javascript

onload = function ()
{
    var e = document.getElementById('actionUrl');
    e.oninput = changeAction;
};

or with jQuery

$(document).ready(function() 
{
    $("input[id=actionUrl]").bind('input propertychange change', function() { changeAction()});
});

set your FORM action in function

function changeAction()
{
    document.getElementById("stash").action = document.getElementById("actionUrl").value;
}

$(document).ready(function() 
{
	$("input[id=actionUrl]").bind('input propertychange change', function() { changeAction()});
});

function changeAction()
{
	document.getElementById("stash").action = document.getElementById("actionUrl").value;
}

function validateUrl()
{
	var _Url = document.getElementById("actionUrl").value;
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	if(regexp.test(_Url)==false)
		return false;
	return true;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id=stash name=stash action="" onsubmit="return validateUrl();">
	<input type=text id=actionUrl value='' />
	<input type=submit id=_submit value='Submit' />
</form>