output result from php [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am getting confused as I read various posts about the typical task I am trying to perform, no doubt countless others have done it before. So at this point in 2013 what is the best practice solution to achieve the following:
I have an html page which asks the user for a number of values, x1, x2, .....xn.
There may be 10 to 20 such inputs. As such I don't want them to be lost or to have to be re-inputted again for whatever reason.
perform calculations based on user inputs then output the results (eg. y1, y2, y3..y5).
As suggested by some of the posts here, I tried "echo" (for PHP), that works fine. However, it shows the results in a new page so the user input is no longer visible.
I want the user to see both - their input and the resultant. That way they can print if they want to and see all info on one page.
I prefer to use "basic" technologies, ie. technologies that most users can use, that they don't have to click OK on some warning, change some setting on their browser, accept some thing or other etc..
Thanks in advance!

Create inputs with html.
Choose between reloading page or AJAX
If you choose reloading then use
code:
<form action="nextfile.php" method="POST">
<input type="text" value="" name="y1" />
<input type="text" value="" name="y2" />
<input type="submit" value="calc me" name="submit" />
</form>
Then in nextfile.php you need get values with $_POST and if you want them to be saved use $_SESSION
For example
<?php
session_start();
if(isset($_POST['y1']) && isset($_POST['y2']))
{
$_SESSION['res'] = (int)$_POST['y1'] * (int)$_POST['y2'];
}
The code above will perform calculation on two inputs with name y1 and y2 and save them in session.
If you want AJAX then you need to visit this page and see examples
You should think of JavaScript solution because it will fit your needs and no server code is needed.

The simplest way is to submit the form to the same page and repopulate the input fields:
// calc.php
<?php
if (isset($_POST['foo'])) {
echo 'Result: ', $_POST['foo'] + 1;
}
?>
<form action="calc.php" ...>
<input name="foo" value="<?php if (isset($_POST['foo'])) echo htmlspecialchars($_POST['foo']); ?>">
...
</form>
The more modern version would be to submit the calculations via AJAX and populate the result via Javascript without reloading the page.

Related

Store html form answer as a variable to be used back on the form-page [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm really new at using PHP, and I'm not really sure what kind of solution I need to be looking for, but anyway...
I have created a mini-form, that only has one question. I have used a select-tag and seven option-tags. I want to save the answer from the user as a variable, that I can then use in an if-statement later, on the same page as the form was on. Is this even possible? And newbie-friendly?
Yes, it is possible.
1. Check that you have added method in form tag (action means that you will be using same page for your if-statement) and that you have name for select element:
<form action="?" method="get">
<select name="question">
<option value="1">one</option>
<option value="2">two</option>
...
<option value="7">seven</option>
</select>
Then, after form submit the page will be reloaded and you will get selected value from $_GET["question"] variable.
Read more about it: PHP $_GET
Using only PHP and HTML, it is not possible to pass a value back to the PHP script once the page has been sent to the user.
What you can do is to configure the form to be submitted to the same page. This way, you will be able to access the variable the next time your PHP script is executed.
For example, your form could look like this:
<form method="POST">
<input type="text" name="userinput" />
<input type="submit" name="submit" />
</form>
After the user has pressed the submit button on this form, the values will be sent to the current page, where you can process them with PHP. They will be located inside the $_POST or $_GET arrays:
<?
if(isset($_POST['submit']) {
processTheUserInput($_POST['userinput']);
}
// other PHP code
?>
// your HTML code

Update a column in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to look for a way to update a column.
Like here:
Database: temptestsite
Table: firstsection
then there is username, heading etc (not a log in type situation).
Say user sends in the username and I want them to be able to change it. How does one UPDATE this specific column. Anything I have tried so far from w3schools inserts a new column. Could someone please make an example of how to do this with PHP?
Like let there be a text box and then a button or a hyperlink next to it saying "update" or "change", whatever user types in that text box, gets updated in the "usernames" column in the database.
Your general process will be:
Check if post data is available
If it is, execute a mysql update call
Build the form
Here is some skeleton code:
<?php
$link = mysqli_connect("myhost","myuser","mypassw","mybd");
$id = $_GET['id'];
if (!empty($_POST)) {
$link->query("UPDATE users SET username = '".$link->real_escape_string($_POST['username'])."' WHERE id = '".$link->real_escape_string($id)."'");
}
$user = mysqli_fetch_assoc($link->query("SELECT * FROM users WHERE id = '".$link->real_escape_string($id)."'"));
?>
<form method="POST">
<input type="text" name="username" value="<?php echo $user['username'] ?>"/>
<input type="submit" value="Update"/>
</form>
There is two update method in rest system,
One of them is PUT , other one is PATCH,
Most frameworks make that request via hidden _method attr, like in laravel
<form method="POST">
<input type="hidden" name="_method" value="PUT"/>
<input type="text" name="column"/>
<input type="submit" value="Update"/>
</form>
PUT is using for changing entire column,
PATCH is just patching as you can understand,
First understand and manage what you really need, then watch & read about them.
As an answer maxhud is explained code version of it.
But it is not the good way to make it with post request.

add / remove field dynamically in php mysql without javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am working on student database. Each Applicant can apply for upto three programs. I want to include add / remove program option on my php form without using javascript. Is it possible to do it wihout javascript?
please help!!!
You can do simple form to add info to Database with Submit Button that's for adding . For Deleting you can make Delete page that GET user id that you want to delete .
PHP only executes at runtime, ie when the page first loads. To do this, you'd need to reload the page every time you wanted a change to the page. So what I see is you have the first page they land on offer a form for the user to choose which of your three programs they're interested in, when the form submits, the page reloads and the server - queuing off the user's selection - loads and displays the appropriate program form.
If you want to do it without reloading the page, it is not possible without JavaScript as PHP cannot execute without talking to the server.
Here's an example of a form that at first displays a question to the user, posts to itself, then conditionally displays a field based on the user's input:
<?php if (!isset($_POST['program'])) : ?>
<form method="POST" action="">
Should we show the program field?<br>
<input type="radio" value="y" name="program" id="program_y"><label for="program_y">Yes</label><br>
<input type="radio" value="n" name="program" id="program_n"><label for="program_n">No</label><br>
<input type="submit">
</form>
<?php else : ?>
<form method="POST" action="form_submit_url_here">
<label for="name">Name:</label>
<input name="name" type="text" id="name"><br>
<?php if ($_POST['program'] === 'y') : ?>
<label for="program_name">Name of your program:</label>
<input type="text" name="program_name" id="program_name"><br>
<?php endif; ?>
<input type="submit">
</form>
<?php endif; ?>
Short answer: you can't.
PHP requires information to be processed either by that page or by another, and therefore data needs to be sent. Javascript (using AJAX) can be used to send that data from the page without a page refresh or relocation.

How to Change data based on keyword [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Suppose i have an array which contains 500 names and there mobile number...
Eg -
$ar=array("98738383839"=>"name1","4343243332233"=>"name2")
Now what i want to do is give a list of 4-5 people with radio button for the user to select 1... It can me starting 4-5 peoples from the array..
Now i want to give the a search box where they can start writing name and the list of 4-5 friends with radio button changes according to that only...
For example
I would recommend you look at angular as it is much easier than jQuery. I am currently doing a project in angular, and here is how I would approach it.
Go to: http://angularjs.org, then scroll down and look at example 3. The search is very powerful and easy to implement. Implement a simple type ahead in angular, which searches the array as user type.
For the boxes with radio button, it can be easily done in angular since it allows bidirectional data binding, which means if u change a value in JavaScript variable it would also change in HTML.
Basic introduction
For your text box include something like this in HTML. This will look for an array called itemLists (you will need to define as $scope.itemLists in your controller).
<input type="text" ng-model="searchRecord" placeholder="Enter to search..." typeahead="itemList.item_id as itemList.name for itemList in itemLists | filter:$viewValue">
Then you need a http request that looks something like the following. The PHP file will query mysql and return a set a results.
$http.get("someFile.php").success(function(response) {
if(response){# store results into itemLists array}
});
Then for the boxes, you create soemthing using CSS + or bootstrap either way, get it to look how you want it to look. Then for images, Email and radio button. Use ng-model just HTML input textbox in the html example code above.
<input type="radio" ng-model="radioButton1">
<input type="radio" ng-model="radioButton2">
...
ng-model is a bidirectional binding variable, you can access it both from HTML and JS. The value is instantly updated no matter where you change it.
Hope it helps, if you need more information the angular site is always a good place to start.

better to use jquery on form submit button? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Is it better to use JQuery for form submission? For example:
<input type="text" id="username" name="username" maxlength="30" /><br />
<input type="password" id="password" name="password" maxlength="30" /><br />
<input type="button" id="register" name="register" value="Register" />
I could bind id="register" with JQuery and then get the values of username and password with val(); when it's clicked and pass them onto a PHP file for a database query with AJAX - this would prevent the page refreshing and makes the user experience more smooth.
Do you agree?
This really depends on your specific scenario. If you're going to be redirecting the user to a different page once they've registered, then it makes no sense to use AJAX.
So what you're actually saying is whether or not using AJAX.
It really is up to what you're looking to do. It can be a good idea if this way you don't have to reload a whole page; but it really is up to you.
On a register page, if the only thing displayed is the registration form, it would be better not to use AJAX. it wouldn't make sense since the whole page has to be reloaded after submitting (again it depends on the scenario).
jQuery has nothing to do with it. In your case the issue is that if one doesn't have javascript activated, they won't be able to register.
You still can use a classic and this kind of javascript:
$("#register").click(function(e){
e.preventdefault();
// then perform your AJAX submission
});
This way, if javascript's not activated, you still can add a classic post submission and register the user.
Better? In what way? It's certainly possible and definitely not new, maybe better - maybe not. I'm not sure what your trying to find out.. Whether it is safe? It can be if done right. But you have to be more specific.
There is a lot of information on the internet on the debate around what AJAX does for the web user experience and opinions are split on both sides.
There is one big limitation with using javascript - the browser's security model. URLs beginning with "http:" count as a different domain as "https:". That means that you can't send credentials over https if the user is currently on a plain http page.

Categories