I am working on an "Update User Data" form, which should reflect the initially entered information stored in the database into the textarea, and can be changed if the user wishes to. While I'm doing so, I have a concern - is directly writing value = <?php //some code ?> the safest bet?
I was trying to invoke a function to place my PHP code in that instead, but apparently it's just displaying the function's name.
Here's my code snippet:
<div>Date of Birth: </div>
<input id="dob" type="date" onFocus="emptyElement('status')" value="reflect_data('dob');">
where the function reflect_data is defined as -
function reflect_data(elem) {
//query and some code to access the data and store into $member
if (elem == "dob") {
echo $member['DOB'];
exit();
}
NOTE : A newbie to PHP, any advice would be welcome. Thanks. :)
You use php code in a JS function. That won't work that way and is impractical. Simple:
<input id="dob" type="date" onFocus="emptyElement('status')" value="<?php echo htmlspecialchars($member['DOB']); ?>">
is the best solution.
Related
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
I'm creating a login page that accepts a username and then redirects the user to log in. This is currently done, and works with the following Java Script.
function process()
{
var url="https://example.com/users/profile" + document.getElementById("username").value;
location.href=url;
return false;
}
<p>Enter your username</p>
<form onsubmit="return process();">
<input type="text" name="username" id="username">
I'd prefer to do this without using Java Script to support users that disable it, older browsers and to hide this from the source code of the page. The subdirectories are protected anyway but I just want the added compatibility.
I'm attempting to do this with PHP instead;
<form action="/authenticate.php">
<input type="text" name="username" id="username">
I'm using the same form but have created a page called authenticate.php instead. All that authenticate.php contains is;
<p>Authenticating…</p>
<?php
$username = ["username"];
header("Location: https://example.com/users/profile/$username"); die();
?>
If steve was the input, I'd expect that to then redirect to https://example.com/users/profile/steve but it doesn't. I've set up redirects already to handle errors and the form translates text to lowercase anyway.
I'm just wondering why;
<?php
$username = ["username"];
header("Location: https://example.com/users/profile/$username"); die();
?>
won't work with the addition to the URL but does work without the $username so that's the only error. I also tried $username = $POST_["username"]; but that's not relevant and doesn't seem to work either. The current code takes me to https://example.com/users/profile/Array
If someone could advise on the correct way to do this I'd very much appreciate it.
By default form method is GET but the best practice is to mention it so you've to do:
<form action="/authenticate.php" method="get">
<input type="text" name="username" id="username">
</form>
And authenticate.php you need to get input value:
<p>Authenticating…</p>
<?php
$username = $_GET["username"];
header("Location: https://example.com/users/profile/$username");
die();
?>
I am developing a web application, and I need to display a live calculation as soon as the fields are filled with the information.
What I mean by a live calculation is something as live as a ng-model in a ng-app. The problem goes like this:
The user chooses a product from a dropdown list, which list's choices are coming from a mysql database and the price of the chosen product should be the first multiplyer, while the second multiplyer is the next input field where the customer should enter how many pieces they want.
I tried to take the price from the table where product is selected but I believe that php needs a submission in order to process the data and actually see which product has been selected.
So is there any way to display a live calculation and inform the customer how much their order will cost before they submit the form?
Thank you!
Update:
I came few steps further along the way but i got stuck again... so now i do retrieve the value of the chosen product but i fail to do the calculation because javascript says that the retrieved value is not a number (NaN) as soon as i try to do the operation.
This is the code that shows the value of the chosen product
<form action="#" method="POST">
<input type="text" name="clientname" class="fields" placeholder="Enter your name " required/>
<input type="text" name="contactinfo" class="fields" placeholder="Phone or Email" required/>
<input type="text" name="address" class="fields" placeholder="Street Address" required/>
<select class='dropdown' name='ordertype' id='ordertype' onchange='productPrice()' required>
<option value="" disabled selected>Select Product</option>
<?php
$query = "SELECT * FROM products";
$query_run = mysqli_query($mysqli, $query);
while($row = $query_run->fetch_assoc()){
echo"<option value=".$row['price'].">".$row['name']."</option>";
}
?>
</select>
<input ng-model="price" type="number" name="amount" class="fields" placeholder="How many pieces?" required/>
<button name="placeorder" class="regbutton">Place Order</button>
</form>
<script type="text/javascript">
function productPrice(){
var option = document.getElementById("ordertype").selectedIndex;
var value = document.getElementById("ordertype").options;
var result = (value[option].value);
document.getElementById("result").innerHTML = result + "$";
}
document.write('<p id="result"></p>' );
</script>
but the moment i try to do the calculation and multiply the given output * {{price}} or even when i try to multiply it * 2 it says NaN on page load (it has nothing to do with the dollar sign).
So do you know why is this happening and how can i fix it?
Thanks again!
Your PHP code isn't going to know when the data changes, so even though you could use comet or websockets to get data immediately from the PHP code to the browser, you still need to poll the database for changes. It's simpler to just trigger the polling on a schedule from the browser using Ajax.
This will give you idea,
var source = new EventSource("demo_sse.php");
source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "<br>";
};
<div id="result"></div>
demo_sse.php
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$time = date('r');
echo "data: The server time is: {$time}\n\n";
flush();
?>
use jQuery ajax call.
go step by step.
1st get the qty of any product or any thing that the have changed.
on ajax call i guess you are creating a cart. let every thing same just add product or any thing to the cart of your. and get the hole data.
append the recent value.
in this way the amount you have stored in php that you can have at your client side using ajax call
I have configured the javascript code to do auto submit but what I want is that if the authentication fails, I do not do the autosubmit again.
My code is the following:
Form:
<?php echo form_open($this->uri->uri_string(), array('class' => 'login-form')); ?>
<div class="form-group">
<label for="email"><?php echo _l('clients_login_email'); ?></label>
<input type="text" autofocus="true" class="form-control" name="email" id="email">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<label for="password"><?php echo _l('clients_login_password'); ?></label>
<input type="password" class="form-control" name="password" id="password">
<?php echo form_error('password'); ?>
</div>
<?php echo form_close(); ?>
Javascript
<script type="text/javascript">
window.onload=function(){
var auto = setTimeout(function(){ autoRefresh(); }, 100);
function submitform(){
document.forms["login-form"].submit();
}
function autoRefresh(){
clearTimeout(auto);
auto = setTimeout(function(){ submitform(); autoRefresh(); }, 10000);
}
}
</script>
How can I do it?
I believe your problem lies in array('class' => 'login-form').
Looking at the documentation for document.forms, you access the forms with ID.
I am not familiar with code-igniter; however, your code tells me that you are probably setting the class for a form. You would need to set the id for a form.
For preventing the auto-submit from running twice. From what I see, I suspect that you are getting a normal HTML form at the end. When you submit a HTML form it would make a trip to the server and when it comes back it should reload the page (unless you make it an asynchronous form).
Since the page is reloading, the window.onload would be run every time. To prevent this you would have to use a true-false flag some wheres.
Here are some potential solutions:
You could try looking into accessing url parameters from JavaScript. Some quick searching shows that this is a lot more complex than I'd expect though...
Alternatively, you could move the JavaScript code into a <script> block and echo out a script block from PHP. If you are going with this option you should look into using addEventListener method instead of accessing onload directly. PHP would make it much easier to accessing URL parameters by using $_GET or $_POST.
A second alternative would be to echo out a <input type="hidden"> that holds the true/false value and then access the value using JavaScript.
Once you can use your flag, you just need to check the flag in order to decide whether or not to auto-submit or not.
if (myFlag){
//submit form
}else{
//don't submit form
}
This true-false value is not sensitive data so it should be safe to place it as a GET parameter.
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
}