-1

I have below code. I don't know much about ajax. I want below input s_amount to convert as a php variable before submitting the form that it is in. There are some syntax errors where param1 in php code is not recognised. I don't know my order is correct or not. How I get this work.

<input type="text" name="s_amount" >
<script>
    $.ajax({
    url: 'payment.php',
    type: 'POST',
    dataType: 'text',
    data: {param1:$("input[type='text'[name='s_amount']").val()},
    })
    .done(function(response) {
        console.log("response");
        responsen=response+1;
    })
    .fail(function() {
    console.log("error");
    })
    .always(function() {
    console.log("complete");
    });
   $param1=done(param1);
</script>

<?php
    $myphpvariable= $_POST['param1'];
    echo 'this is my php variable: '.$myphpvariable;
?>
cweiske
  • 30,033
  • 14
  • 133
  • 194
user3138830
  • 63
  • 1
  • 2
  • 6

1 Answers1

0

The below code will help you in understanding the concept.

 <input type="text" name="s_amount" >
 <script>


 $.ajax({
     type: "POST",
    context: "application/json",
    url: "payment.php",
    data:{param1:$("input[name='s_amount']").val();},
    success: function(msg) 
    {
         alert(msg);
    }
    })

 </script>

payment.php

 <?php
     $myphpvariable= $_POST['param1'];
      echo 'this is my php variable: '.$myphpvariable;
 ?>

The main thing here was the line

  {param1:$("input[type='text'[name='s_amount']").val()}

it was not fetching the value from the texbox. change it to

  {param1:$("input[name='s_amount']").val();}

I hope you wanted to achieve this.

Gourav
  • 1,765
  • 2
  • 15
  • 16