Get Input Value on the same page with PHP? - javascript

Well, first i'm new in PHP. Is there a way to get the input value from a existing input on the page on page load with php and pass it to a variable?
For example i have this input: <input type="text" name="g_id_p" id="example1" value"foo">
I want to do something like this: $got_it = $_GET['g_id_p'];
Sorry again if i wrote my code wrong, im noobie on this. Hope to someone help me.

First, would be great to know what method is the form. (GET or POST)
Then after know what type of method you could call it in PHP:
METHOD POST:
<input type="text" name="g_id_p" id="example1" value"foo">
$variable = $_POST['g_id_p'];
METHOD GET:
<input type="text" name="g_id_p" id="example1" value"foo">
$variable = $_GET['g_id_p'];
If you haven't defined a method, in html the tag for a form is:
<form>
<!-- Here goes your input and some stuff -->
</form>
Then it would be something like:
<form name="form_name" class="form_class" id="form_id" method="TheFormMethod" action="ThePageThatExecutesThisForm">
<!-- Here goes your input and some stuff -->
</form>
TheFormMethod can be post, get, delete, put.

You can't really get an associated value of an input tag within the same PHP page but what you can do is set the value of a variable beforehand.
What I mean is, create an array that will store all the values of all the input tags.
$inputValues = array();
$inputValues['g_id_p'] = 'foo';
Then when you have the tag later on just echo it from the PHP var.
<input type="text" name="g_id_p" id="example1" value="<?php echo $inputValues['g_id_p']; ?>">
As you can see, we aren't really 'getting' the value that you set but the end result is the same.

You have to check if the values is set isset(), where you want to use the variable do:
if(isset($_GET['g_id_p'])){
//your code
}

Related

Get value of input field using php into variable

I have already refered to this question and the accepted answer did not work for me: How to get input field value using PHP
This is my code result.php file:
...
<th>
<form name="form" action='checkout.php' method='POST'>
<input class='mx-2' type='number' id='price' name='price' placeholder='Donation Amount'">
</form>
</th>
<script
src='https://checkout.stripe.com/checkout.js' class='stripe-button'
data-key='key'
data-amount=get value of input field with id price here
data-name='Name'
data-description='Description'
data-currency='usd'
data-locale='auto'>
</script>
...
I have also tried fetching the value of the input like this and then using that variable:
<?php $price = isset($POST['price']) ? $POST['price'] : 0; ?>
Another method I tried:
<?php
$htmlEle = "<span id='SpanID'>Span Sports</span>";
$domdoc = new DOMDocument();
$domdoc->loadHTML($htmlEle);
$spanValue = $domdoc->getElementById('SpanID')->nodeValue;
I found the above snippet on https://phpcoder.tech/how-to-get-html-tag-value-in-php/ and modified it as per my need but did not work.
How can I do this? My app is pay what you want so I want the price to be filled in by the user on the client side.
I am open to different approaches and solutions to the one I asked for.
If you don't want to wait for the client to submit the form, you will need some javascript as PHP is a server-rendering language.
Basically you would need to set-up a listener on the input and after the client types the data in a format you want and you validate it, you can pass that to stripe script.
<th>
<form name="form" action='checkout.php' method='POST'>
<input class='mx-2' type='number' id='price' name='price' placeholder='Donation Amount'">
<input type="submit">
</form>
</th>
submit button in missing here, so add a submit button.
And update your PHP code
<?php $price = isset($_POST['price']) ? $_POST['price'] : 0; ?>
$_POST - is PHP Superglobals for getting form's post values.
$POST (which you are using) is a normal PHP variable.
Please update your code like this.It will works.
You have used the wrong POST syntax, the correct is: $_POST, while you are trying get: $POST.
The docs: https://www.php.net/manual/en/reserved.variables.post.php
--In JavaScript part--
If you want to handle PHP the price before call Stripe, you should use other configuration, because this one will not work anyway.
You can:
call Stripe on like ajax or other request that in background post the forst
call Stripe on other page
don't do form, just plain text field (if you don't need PHP, handle price before Stripe request)
It depends what you want to do.
I used a different method to solve the problem. I put the text box on a separate page and the pay button on the checkout page, so I am passing the price from the previous page to the checkout page now and accessing it using query parameters
<form name="form" action='checkout.php' method='POST'>
<input class='mx-2' type='number' id='price' name='price' placeholder='Donation Amount'">
<button name="submit" type="submit" >submit button</button>
</form>
///in checkout.php code
if(isset($_POST['submit']){
$price = $_POST['price'];
echo $price;
}
///I hope this would work

Javascript: How to print a value from a $_SESSION array?

Is there a way to have javascript create php code, to be executed when it runs on the php page? I'm trying to set the value of an input to , where div_id is a js variable, and inputs is a 2D associative array.
The problem is that it literally sets the value to "" instead of the value of the floor_type field. Everything else seems to work.
inputs_div.innerHTML += `<div id="`+div_id+`" class="dynamic_div">
<ul class="dynamic_ul">
<li><input type="text" onblur="$('.save').click();" placeholder="Flooring Type: Wood Floor, Tile, etc." name="inputs[`+div_id+`][floor_type]" maxlength="255" value="<?php echo $_SESSION['inputs']['`+div_id+`']['floor_type']; ?>"></li>
</ul>
</div>
I'm creating some html inputs dynamically with javascript, but I'm looking for a way to prevent them from getting cleared if the page is refreshed. Maybe there is a simpler way to make the inputs stay there?
My idea was to use ajax to run some php code from the onblur event of each input element, which saves the inputs[][] array into the session, so it doesn't get lost when refreshing the page.
Use a hidden field to store $_SESSION[] value and then on document ready get the value of hidden field and set to the javascript variable.
<input type="hidden" id="hdn" value="<? php echo $_SESSION['inputs'] ?>" />
$(document).ready(function()
{ var session_value = $('#hdn').attr('value'); // now use session_value variable when set the innerhtml of inputs_div });

Pass a global javascript variable into argument of an action form

I would like to include a global javascript variable that it is dynamically updated into the argument of an action form.
Previously, I was simply using django template syntax to include the django variable string returned by the view into the action form but now that it is updated I need to come up with a way to pass this updated javascript variable into the GET method.
I save the variable as a window property "window.subgraph_query = 'foo'" this variable is supposed to change over time so it needs to be updated also in the form argument as well. How can I reach this? I think it should be easy
Thank you!
<script type="text/javascript">
// BEFORE
var subgraph_query = "{{ subgraph_query }}";
//NOW
window.subgraph_query = "{{ subgraph_query }}";
</script>
// BEFORE
<form action="/graph/network_to_gml/{{ subgraph_query }}" method="get">
<input name="export-node-list" id="export-node-list"type="text"/>
</form>
// NOW
<form action="/graph/network_to_gml/JAVASCRIPT_VARIABLE" method="get">
<input name="export-node-list" id="export-node-list"type="text"/>
</form>
You need to add a hidden input field to your form and assign a value of a js variable. this way it won't be visible for a user but your script will receive it.
Example of a hidden field type:
<input type="hidden" name="country" id='hidden1' value="">
and in JS:
document.getElementById('hidden1').value = 'Poland';

Get variable by user input and use the variable in form URL

I have a problem with using variables in URL's. The reason I need to do this in JavaScript is because I can't use PHP (Phonegap).
<form action="https://website.com/login.dll?Logon" method="post" id="logonForm">
<input type="hidden" id="curl" name="curl" value="Z2FZ2Fexample.phpZ3FZ26idZ3D<?php echo $_SESSION['user_email'];?>Z26typeZ3D0" />
This works, but now I am using Phonegap so I can't use PHP, So I need to get a variable set by the user and then use that variable in the URL (instead of the php echo) I guess. I'll still be using the same form.
So I need the user to enter the info I need and pass that to a variable,
<input id="data" type="text" name="data" placeholder="Placeholder" required />
What is the best way of doing this in Javascript?
Add another attribute to the form
onSubmit = 'setCurlValue();'
Add code for setCurlValue
function setCurlValue() {
$('#curl').val('Z2FZ2Fexample.phpZ3FZ26idZ3D'
+ $('#data').val()
+ ';?>Z26typeZ3D0');
}

Passing javascript variable to html textbox

i need help on html form.
I got a javascript variable, and I am trying to pass the variable to a html form texbox. I want to display the variable on the textbox dynamically. but i do not know how to pass the variable to the html form and call the variable?
var test;
<INPUT TYPE="TEXT" NAME="lg" VALUE="" SIZE="25" MAXLENGTH="50" disabled="disabled"><BR><BR>
How do i pass test to html form and change its value?
Thanks
Pass the variable to the form element like this
your form element
<input type="text" id="mytext">
javascript
var test = "Hello";
document.getElementById("mytext").value = test;//Now you get the js variable inside your form element
<form name="input" action="some.php" method="post">
<input type="text" name="user" id="mytext">
<input type="submit" value="Submit">
</form>
<script>
var w = someValue;
document.getElementById("mytext").value = w;
</script>
//php on some.php page
echo $_POST['user'];
instead of
document.getElementById("txtBillingGroupName").value = groupName;
You can use
$("#txtBillingGroupName").val(groupName);
instead of groupName you can pass string value like "Group1"
This was a problem for me, too. One reason for doing this (in my case) was that I needed to convert a client-side event (a javascript variable being modified) to a server-side variable (for that variable to be used in php). Hence populating a form with a javascript variable (eg a sessionStorage key/value) and converting it to a $_POST variable.
<form name='formName'>
<input name='inputName'>
</form>
<script>
document.formName.inputName.value=var
</script>
You could also use to localStorage feature of HTML5 to store your test value and then access it at any other point in your website by using the localStorage.getItem() method. To see how this works you should look at the w3schools explanation or the explanation from the Opera Developer website. Hope this helps.
document.getElementById("txtBillingGroupName").value = groupName;

Categories