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

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.

Related

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.

output result from 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 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.

Random challenge in a form?

So right now what I am trying is to randomly get a question from a set of questions in a table on the database and display them on the form for the person to answer. if that is a bad thing to do please let me know how I should go about it instead. I guess it leaves an easy way to abuse the database, perhaps? I'm not sure.
Well, right now I have the form and the form processing on the same page. The problem is that when it is submitted, it of course refreshes and changes the question, which means unless they get lucky, the answer is wrong. I am not sure how to go about setting it up so that I can generate the random question and make sure when they submit the form that it works out correctly without changing the answer. i have form validation with javascript set up, but that won't help against bots.
So basically, what I have is
check stuff, connect to databse, etc
$random = mysql_fetch_row(mysql_query("select * from questions order by rand() limit 1"));
clean submitted data, check, submit, etc.
In the form, I have
General form stuff, name, email, etc
<label for="question"><?php echo $random_row[1]; ?></label>
<input type="text" name="question" id="question" required="required"/>
So you can see what I am wanting to do, if that wasn't clear before.
Your effort seems perfect.
You need to store question id in session because when you submiting form data in next page you require question which was asked.
So you will get question detail by question id store in session and compare answer on submitted page.
Hope this may help.
If you really want to handle the input and the submission with the same page, you're going to end up with a structure like this.
if (!empty($_POST))
{
// there is $_POST data, so there must be a submission
}
else
{
// there is no $_POST data, so the user needs to input
}
To know which question the user is answering in the submission, add a hidden input to the form.
<form ...>
<input type="hidden" name="question_id" value="<?php echo $question_id; ?>" />
<input type="text" name="answer" />
</form>
When receiving the submission, you can use the question_id to lookup the correct answer in the database. Compare the correct answer to the answer the user gave.
Please ask more specifically if you require detail about a particular part.

jquery page reset on php form submission

So I am making a small quiz app that shows one question at a time, in order to move on to the next question you have to answer the current one correctly, and you have as many tries as it takes you...The quiz works perfectly. When you have answered the last question correctly, a form is displayed and it asks you to fill out some information so that the "quizzer" can send you a gift...The submission of this form is done via php, I did plan on implement JQuery/AJAX but right now I am just testing out the DB connectivity and other functions so i kept it simple...the problem I am having is that when I hit submit the page automatically gets refreshed showing the first question again and not outputting anything from my php script..I am not really sure what kind of code I could include to help anyone solve this, as I think it is a more theoretical problem, but let me know if you want me to post more...
Thanks in advance for any help, it is greatly appreciated!
<form action"/index.php" method="POST">
Congratulations! You passed! Please enter in your email address and we will send you something cool!<br />
<input type="text" name="email" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
If the page submits and posts back to itself, the form is going to reset by design. You have to add code that detects the postback and if "True" then repopulate the form with the postback values to give the appearance of data persistance.
Havne't used PHP in a long time, but in .net you would detect a postback by calling:
....If IsPostBack Then....
When you do a postback, the entire page reloads, so that is going to fire your Javascript code again.
To avoid that, you should encapsulate just the form with AJAX to submit just the form so that the rest of the page doesn't post back (including the javascript).
Hope this gives you some direction.
Or... you can change
to post to a different file (like /index2.php) and that way you don't have to worry about postbacks or stuff like that.
Did you check the post data on the same index.php page, like doing:
if(isset($_POST) AND !empty($_POST)) {
var_dump($_POST);
}
This might help you in some way.
I am having the same issue, but with jQuery Mobile. I have implemented this on the form submit but all I need to do refresh the query results:
onClick="window.location.reload(true)"
This is basically a page refresh more than anything but might point you on the direction you need as well.

Categories