0

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.

4
  • 3
    Just submit the form and get it using $_REQUEST/$_GET/$_POST Commented Dec 27, 2013 at 12:02
  • 1
    BY <form> you can post data then get that form data in $_REQUEST. Commented Dec 27, 2013 at 12:04
  • 1
    I want this thing happen before submit the form. I want to manipulate the data before submitting Commented Dec 27, 2013 at 12:14
  • you may use ajax to do this Commented Oct 19, 2018 at 11:46

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

followed your steps but getting Undefined index: param1
3

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'];
   ...

1 Comment

I'd say if the poster is just getting to grips with HTML/PHP, then this is a good place to start.
1

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.

Comments

0

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'];

Comments

0

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'];

2 Comments

Even though this is a more advanced way to save post data by using a database. The answer you're providing is totally out of scope. And that is why i down voted your answer.
Lolx i just thought about maybe what he will want to do

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.