I have a random string using a php function I found here
<?php function generateRandomString($length = 12) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$randomString;
}
$randomString = generateRandomString(); ?>
I have a form
<form method="post" id="form" action="<?php bloginfo('url'); ?>/results/">
<input type="text" name="height" placeholder="Enter height">
<input type="text" name="weight" placeholder="Enter weight">
<input type="submit" id="export">
</form>
When you click submit it takes you to the .com/results/ page but i am trying to change /results/ to the random string from the generateRandomString function and still show the content that would have been shown on the results page
To do this so far i have tried adding this jQuery under the form
$("#export").click(function(){
url = $("#form").attr("action");
url = url.replace("/results/",<?php echo $randomString ?>);
$("#form").attr("action", url).submit();
});
but that still doesn't work. Does anyone know how another way i could do this please?
Thank you
the php file below-
<script src="jquery-3.1.1.min.js"></script>
<?php function generateRandomString($length = 12) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
$randomString = generateRandomString();
?>
<form method="post" id="form" action="<?php bloginfo('url'); ?>/results/">
<input type="text" name="height" placeholder="Enter height">
<input type="text" name="weight" placeholder="Enter weight">
<input type="submit" id="export">
</form>
<script>
$("#export").click(function(){
url = $("#form").attr("action");
url = url.replace("/results/",<?php echo $randomString ?>);
$('#form').attr('action', url).submit();
});
</script>