Reputation: 63
Is there any method to take value of the input to a php variable.
<input type="text" name="s_amount" style='width:20%;' required>
I want value of this s_amount to take into a php variable $s_amount. This code is already inside a form. I want this conversion happen before submitting the form.
Upvotes: 0
Views: 31202
Reputation: 391
This is for form
<form action="process.php" method="POST">
<input type="text" id="table_name" name="table_name" placeholder="name" />
<input type="submit" name="create_table" id="create_table" value="create a table"/>
</form>
This is in process.php
$table_name = $_POST['table_name'];
Upvotes: 0
Reputation: 423
Just submit the form and get it using $_REQUEST/$_GET/$_POST
.
HTML:
<form method="POST">
<input type="text" name="s_amount" style="width:20%;" required>
<input type="submit" name="submit" value="Submit" />
</form>
PHP:
<?php
...
$value_in_php_var = $_POST['s_amount'];
...
Upvotes: 3
Reputation: 7026
You can't convert a javascript variable into a php variable without communication with the server. So if you would like to turn a client side variable (your input value) into a php variable BEFORE submitting a form you'll need to post/get this variable to the server in a separate request. The best way to do this is by an asynchronous ajax call. If you are using jquery this can be very simple:
$.ajax({
url: '/path/to/file.php',
type: 'POST',
dataType: 'text',
data: {param1: $("input[type='text'][name='s_amount']").val()},
})
.done(function(response) {
console.log("response");
//do something with the response
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
On the server you can receive this value by getting the posted data like so
$myphpvariable= $_POST['param1'];
//here you can manipulate/validate your variable and echo something back to the client
echo 'this is my php variable: '.$myphpvariable;
Now your ajax call will receive "this is my php variable: [x]" in its .done() callback.
EDIT: don't forget you have a javascript console in your browser (for example firebug). this can be very handy to see what's going on.
Upvotes: 3
Reputation: 10754
Change the HTML markup to:
<form method='POST'>
<input type="text" name="s_amount" style='width:20%;' required>
<input type="submit" name="submit" value="Submit" />
</form>
Then, submit the form, and after that you can extract
the $_POST
variable, but note that it will create variables for each key in that variable.
extract($_POST);
echo $s_amount;
Otherwise, you can submit the form, and simply do:
$s_amount = $_POST['s_amount'];
Upvotes: 0
Reputation: 1229
PHP runs on the server. So in order to get the value to the script, you either have to use AJAX or you have to submit the form.
Upvotes: 1