1

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>
3
  • try url = url.replace("/results/","<?php echo $randomString ?>"); Commented Mar 1, 2017 at 11:28
  • @AlexOdenthal that still takes you to /results/. It does give an error saying $ is not defined in console, but I put in script tags (it's in php file) and added jQuery library - don't know why it still say that Commented Mar 1, 2017 at 11:35
  • /results/ is a wordpress page that gets its contents from results.php file. I have it kind of working now!! It will return mydomain.com/random BUT it 404 and says the page isnot found. Any idea how i can send content from results.php to the new url? Commented Mar 1, 2017 at 12:33

2 Answers 2

2

Your function does not return a value, use return statement:

 function generateRandomString($length = 12) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
         $randomString .= $characters[rand(0, $charactersLength - 1)];
    }

    return $randomString;
}
Sign up to request clarification or add additional context in comments.

1 Comment

even with return it still doesnot replace, i have added the php code so you can see
1
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.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="google.com/results/">
    <input type="text" name="height" placeholder="Enter height">
    <input type="text" name="weight" placeholder="Enter weight">

</form>
 <input type="submit" id="export">


<script>
$("#export").click(function(event){
  url = $("#form").attr("action");
  url = url.replace("results/","<?php echo $randomString ?>");
  console.log(url);
 //event.preventDefault();
  $('#form').attr('action', url).submit();
});
</script>

6 Comments

please see the update, still does not work with return in function
you have to write php code in java script using " ". So replace url.replace("/results/",<?php echo $randomString ?>); with url.replace("/results/","<?php echo $randomString ?>");
I appreicate your help sir, but that still just goes to /results/. i am getting these console errors: GET domain.com/jquery-3.1.1.min.js and Uncaught TypeError: $ is not a function. Maybe that is causing the problem?
I executed those code but i didn't get any error . whatever see the update code and run on your server .
I have it kind of working now thanks to you! it is going to mydomain.com/randomString after i click it, but it says the page isnot found. Do you know how i could still send the content from /results/ page (results.php) to the new url?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.