How to call ajax with php its easy step to call ajax lets we go for more we need to pass a url in ajax to call php function/variable/data from php for without refresh the page
$.ajax({
url: "string.php",
method: 'post',
data: {type:'string_action',length: length},
dataType: 'html',
success: function(response){
response=response.replace(/(\r\n|\n|\r)/gm,"");
$('#p_code').val(response);
        
}
});
Example
index.html
<html>
<head>
<title>Random string code Generator using javscript, ajax and php</title>
<script src="/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
  
function getrandonstringcode() {
var length = 8;
$.ajax({
url: "string.php",
method: 'post',
data: {type:'string_action',length: length},
dataType: 'html',
success: function(response){
response=response.replace(/(\r\n|\n|\r)/gm,"");
$('#p_code').val(response);
         
}
});
}
</script>
</head>
<body>
<h1>Generate Random String using characters,numbers and symbols</h1>
<input class="form-control" type="text" id="p_code" name="p_code" value="" placeholder="RandomString"/>
<button type="button" onclick="getrandonstringcode()" class="btn btn-success">Generate</button>
</body>
</html>
   
       
      
     
   
$.ajax({
url: "string.php",
method: 'post',
data: {type:'string_action',length: length},
dataType: 'html',
success: function(response){
response=response.replace(/(\r\n|\n|\r)/gm,"");
$('#p_code').val(response);
}
});
Example
index.html
<html>
<head>
<title>Random string code Generator using javscript, ajax and php</title>
<script src="/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
function getrandonstringcode() {
var length = 8;
$.ajax({
url: "string.php",
method: 'post',
data: {type:'string_action',length: length},
dataType: 'html',
success: function(response){
response=response.replace(/(\r\n|\n|\r)/gm,"");
$('#p_code').val(response);
}
});
}
</script>
</head>
<body>
<h1>Generate Random String using characters,numbers and symbols</h1>
<input class="form-control" type="text" id="p_code" name="p_code" value="" placeholder="RandomString"/>
<button type="button" onclick="getrandonstringcode()" class="btn btn-success">Generate</button>
</body>
</html>
create a string.php
 <?php  
   //only character or number only 
      if($_POST['type'] == 'string_action'){
      $length         = $_POST['length'];
        $useLetters     =  true;
        $useNumbers     =  true;
        $useSymbols     =  false;
        $uppercase    = ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'];
        $lowercase    = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'];
        $numbers      = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        $symbols      = ['`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '\\', '|', '/', '[', ']', '{', '}', '"', "'", ';', ':', '<', '>', ',', '.', '?'];
        $characters   = [];
        $coupon = '';
        if ($useLetters) {
            if ($useMixedCase) {
                $characters = array_merge($characters, $lowercase, $uppercase);
            } else {
                $characters = array_merge($characters, $uppercase);
            }
        }
        if ($useNumbers) {
            $characters = array_merge($characters, $numbers);
        }
        if ($useSymbols) {
            $characters = array_merge($characters, $symbols);
        }
            for ($i = 0; $i < $length; $i++) {
                $randString .= $characters[mt_rand(0, count($characters) - 1)];
            }
     //print coupon and exit go to back to the index file  
       echo $randString;
     exit(0);
  }
?>  
No comments:
Post a Comment