Hey I got a working jquery post function, but it’s still not working as I want it to work. The var “url” should be a an url as in the regex pattern below, when it matches the regex pattern jquery should post a request with the url in it to my php file, after that it will return a unique ref id and rewrite the url to a new one on my own site like example.org/ref.php?id=unique1235 (the url rewrite/replace with unique generated string from php script works already perfectly.)
My JQuery function looks like this:
$(".url2").keyup(validNum).blur(validNum);
var refid;
var url; // needs to be extracted from the live input textarea with the pattern below that is used to replace the old url with the new one coming from the post php script
$.ajax({ url: "/test.php",
data: {
url: url
},
type: "POST",
success:function(returnID) {
refid = returnID
}
});
function validNum() {
var initVal = $(this).val();
outputVal = initVal.replace(/(https?:\/\/)?(www.)?[0-9A-Za-z-]{2,50}\.[a-zA-Z]{2,4}([\/\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#]){0,250}(\s){1}$/
,refid);
if (initVal != outputVal) {
$(this).val(outputVal);
return false;
}}
});
I’m unable to “live”-extract the url from my textarea and save it to “var url” so that it can be passed and send to the php script where the old url gets saved and replace by an unique string (as described above). Would be so glad if someone is able to help me. Thanks alot 🙂
The problem is that you’re triggering the $.ajax() directly, it’s like “Everytime the document loads, that empty request loads, and it doesn’t load more”. Put it on a function, like this:
Now, call the function in the click event of some button, for example:
It’s your turn to modify this to work as you expect 😀
Good Luck 😉
EDIT:
For live checking, you can use this: