stored the several value of the textbox into an array php - php

Im making my project, and my project is make a program that will enter name,age and address.
There was a 3 textbox and a submit button. when the user answer the 3 textbox and click submit button, the data or the input of the user will save into an array, but when the user try to input again the previous input will not be replace or it will be there together with the user new inputs, and later those input where use to save into mysql db, do you think this project is possible using php array? i have no sample code because i dont know how to start.thank you so much.
index.php
<form action='index.php' method='get'>
<input type='text' name='name[]'>
<input type='text' name='age[]'>
<input type='address' name='address[]'>
<input type='submit' value='Save'>
</form>

Simple solution is to use 'sticking form' - form elements that stick (keep) the values previously entered.
You can make a textbox sticky like this:
<input type='text' name='name' value='<?php isset($_POST['name'])? $_POST[\'name\']: ""; ?>' />
You need to sanitize the inputs, however, if you try to use it for some real application.

Related

How to take input value from outside form action without javascript?

I still in training,
But i stuck in this problem.
// we need this password in 2 form POST
<input style='text' name='password' />
<form action='delete.php' method='POST'>
... // i need password to be here
<input type='submit' name='delete' value='Del' />
</form>
<form action='edit.php' method='POST'>
... // and i need password to be here too
<input type='submit' name='edit' value='Edit' />
</form>
My menthor want to do this using 2 form but I need the input password outside this 2 form.
Are there any way to do this without using script?
I can do this with 1 form and action.php that include 2 php file(edit.php and delete.php)
But he said that it is waste.
Can someone help? I already checking using search engine and found nothing.
Maybe your mentor just testing you on how you solve the situation.. As far as i know... you cant pass value of a textbox upon submit without putting the textbox inside a form.. I just wonder why javascript is forbidden.. Well anyway I suggest you to use 1 form and 1 page for the action and in the action make an if statement to separate you delete and edit method.. maybe in that case it is not waste.. It's just only a suggestion..

Handling datas from dynamically generated form

I thought of making something that generates lots of form dynamically and then using jquery to handle their submission.
So i generate the form using php by using something like this.
while(some condition) {
echo "<form method=post action=specific_url.php name=some_form_name>";
echo "<input type=text>";
echo "<input type=submit>";
echo "</form>";
}
Well the above is just a skeleton. The main Problem is how should i name/id the form elements so that i can uniquely identify each form during submission and submit them, and use can extract the data at the specific_url page without any problem.
A direct analogy of this feature that i can think at the top of my head is that of facebook posts, where each post has got a comment box.
UPDATE
I guess i wasn't very clear with the question. Sorry about that. Let me rephrase it.
So till now, I have these dynamically generated forms. Now suppose user writes in one of the textbox and click on submit button.
At these point jquery should take control of it. Send that particular form data to the url. Retrieve the result and do something.
So in the end, I should be able to do something like this
$(some_selector).submit(function(e) {
e.preventDefault();
$.post('specific_url.php',$(some_selector).serialize(),function(data) {
// some stuff
});
});
Here "some_selector" is that selector which gets activated when a particular form is submitted and then sending that form's data.
You can use a hidden form element and set its value to an id for the form.
<input type="hidden" name="form_id" value="1">
This way you can access the form_id like any other form value once you submit it. I am looking at the facebook source now and I see a few hidden form elements next to the comment boxes
It is a requirement that the element id attribute is unique, not so with the name attribute.
It is also possible to use arrays of elements, for example :
<input name="comment[]" type=text>
<input name="comment[]" type=text>
<input name="comment[]" type=text>.
In your PHP. the value of $_POST['comment'] will be send as an array.
If you want to uniquely identify each comment in your PHP code, you will be forced to rename them uniquely.
<input name="comment1" type=text>
<input name="comment2" type=text>
<input name="comment3" type=text>
Since you are generating the form using PHP, this would be easy to keep track of the name that should be generated.
If you have multiple forms, you have free reign, sort of. There is no need a name for the form. However, to identify where in the page the form is places, place a hidden field.
while(some condition) {
echo "<form method=post action=specific_url.php";
echo "<input name='position_in_page' type='hidden' value='$somevalue'>";
echo "<input name='comment' type='text'>";
echo "<input type='submit'>";
echo "</form>";
}

Typing Url loads php and enters empty values in data base. How to avoid?

If i enter the url of my php code it is running directly and submitting the empty values into the data base. I want that php file to run only when submit button is pressed not when entering url directly. please help me
in the beginning of your file make sure to verify that your $_GET or $_POST array (depending on what method are you using for submiting your form) contains some minimal amount of data.
Assuming that you have two input element (name and surname) on your form such as:
<form action='myscript.php' method='post'>
<input type='text' name='firstname' value='' /><br/>
<input type='text' name='surname' value='' /><br/>
<input type='submit' name='do' value=' submit this form ' />
</form>
then on the beginning of your file (in my example it's called myscript.php) you will have something like this:
if (!isset($_POST['firstname']) || strlen($_POST['firstname'])<4)
die("You did not fill in the form properly - first name must be longer than 4
if (!isset($_POST['surname']) || strlen($_POST['surname'])<3)
die("You did not fill in the form properly - surname must be longer than 3 characters!");
//and here continues your code, where you save the surname and first name to the database
Check if one of the variables is set.
if (isset($_POST['oneOfMyVariables'])) {
//run some code only if something or many things are send. Whatever.
}
That will stop it from running unless the variable(s) have been set.

how to auto fill html input using different html input

<html>
<input name='name'>
<input name='handphone'>
</html>
I need some script to make if user type on name and their 'handphone' data that store on db will show on second input form
example if done
<form>
<input name='name' value='surya'>
<input name='handphone' value='081392111098'>
</form>
I think you need that if Any User type name input and if name is in database then headphone input autofill from database. For this You have to Use ajax or jQuery.
thanks

Are inputs in separate forms sent when a submit button is hit?

I'm trying to make a page in php that takes rows from a database, displays them, and then give the viewer a chance to upvote or downvote a specific entry. Here is a snippet:
echo("<form action=\"vote.php\" method=\"post\"> \n");
echo("<INPUT type=\"hidden\" name=\"idnum\" value=\"".$row[0]."\">");
echo("<INPUT type=\"submit\" name=\"up\" value=\"Upvote.\"> \n");
echo("<INPUT type=\"submit\" name=\"down\" value=\"Downvote\"> ");
echo("<form/>\n");
The problem is when I hit a submit button, the value for idnum that gets sent is based on the one farthest down it seems. So my questions is, when a submit button is pressed, are the values for all inputs on a page sent?
Your form tag isn't closed properly. You have <form/>, but it should be </form>.
This makes the entire page a form so it sends all the inputs. With a form that is closed properly though, it will only send the inputs within the form tags that the pressed button was in.
While this wouldn't have helped with this particular issue, I would recommend not mixing your markup with your logic wherever possible. This is quite a lot more readable, and quite a lot more editable as well:
<form action="vote.php" method="post">
<input type="hidden" name="idnum" value="<?php echo $row[0]; ?>">
<input type="submit" name="up" value="Upvote.">
<input type="submit" name="down" value="Downvote">
</form>
Your form is not closed properly. Use </form> instead of <form/>.
The problem with your html is that you should have </form>, not <form/>.

Categories