For a project, I need to generate a dynamic html form that will send POST info to a PHP page via the ACTION field.
The form has not to be static, it has to be dynamic so the user has to be able to generate(ADD) a not fixed(dynamic) number of input tags, then when all the inputs are generated and filled the user may click on submit button and send all the info to a php document via post.
I'm completely lost
I've been playing with this piece of code that generates the inputs but I'm not able to send the data via post to the php file
<script>
var choices=[];
choices[0]="one";
choices[1]="two";
function addInput(divName){
var newDiv=document.createElement('div');
newDiv.innerHTML="<input type='text'>";
newDiv.innerHTML=newDiv.innerHTML+"</input>";
document.getElementById(divName).appendChild(newDiv);
}
</script>
<form class="new" method="post" action="action.php">
<div id="dynamicInput">
</div>
<input type="button" value="Add" onclick="addInput('dynamicInput');" />
<input type="button" value="Save" />
</form>
To submit the form, one possible scenario is to change the type of Save button to submit:
<input type="submit" value="Save" />
Also add name property to your auto generated inputs. Otherwise you won't access the submitted $_POST data:
<input name="enter_name[]" type='text'>
As you see, I'm concatenating [] to the input field. That will convert all submitted data into an array. Otherwise, that last one auto generated input will overwrite the previous entered data.
just change your input type to submit instead of save
<input type="submit" value="Save" />
and you don't need to create new div each time to create an input you can either append it to the div you already have
document.getElementById(divName).innerHTML+= "<input type='text' name= 'added_input[]'/>";
or
var newInput=document.createElement('input');
// if you want each input in separate lines
newInput.style.display = "block";
newInput.setAttribute("name","added_input[]");
document.getElementById(divName).appendChild(newInput);
and in your php file you can get post values like this :
for ($i = 0; $i < $_POST['added_input']; $i++){
echo $_POST['added_input'][$i];
}
for more option to get post values see This Question
Related
i have javascript that changes an input's value and when i post the form, the field is not transferred
<input id="exampleID" type="button" name="exampleButton" value="click me" onclick="changeValue()">
when the button is clicked the value (displayed to the user) changes to "Clicked"
then when i post the form it the value is left behind
if(isset($_POST['exampleButton'])) {
echo "true";
}
this doesn't echo anything
what should i do?
You can change value of hidden field:
<input id="exampleID" type="hidden" name="exampleButton" value="">
<input type="button" value="click me" onclick="changeValue()">
<script>
function changeValue(){
document.getElementById('exampleID').value = "Clicked";
}
</script>
The input button control won't send any value to the server on submit, as it is made to interact with javascript. If you need to send a dynamic value, store it in a field (like an input hidden control), or, if you insist to send it through a button, change its type to submit
I have an input type text box as follows
<input type="text" name="deleteprofileconfirmation" id="deleteprofileconfirmation" class="editprofileinput">
Delete Account
I need to pass the value entered in the input type text to deleteaccount.php
I can do with help of jquery, no problem, i need a pure php solution...
I tried using sessions, but problem is how to read the value in input type when link is clicked.. $_POST is also not working...
i cannot use form because this is a form in another form so html5 is not allowing nested forms, sorry should have mentioned that earlier
the following is not working on deleteaccount.php
if (isset($_POST['deleteprofilebutton']))
{
$delete_profile = strtolower($_POST['deleteprofileconfirmation']);
}
Make your link as
href="../controllers/deleteaccount.php?id=$ID_VALUE"
and update the POST to GET
if (isset($_GET['id']))
{
$delete_profile = strtolower($_GET['id']);
}
That would be GET now.
Make sure users with no privileges can hit this url and delete the profiles. Do check the user rights before processing the delete operation.
You can do this using form
<form action="../controllers/deleteaccount.php" method="post">
<input type="text" name="deleteprofileconfirmation" id="deleteprofileconfirmation" class="editprofileinput">
<input type="submit" class="deleteprofilebutton" name="deleteprofilebutton" id="deleteprofilebutton" value="Delete Account">
</form>
You could also give each one a unique name and just check the $_POST for the existence of that input.
<input type="submit" name="deleteprofileconfirmation" value="Delete" />
<input type="submit" name="submit" value="Submit" />
And in the code:
if (isset($_POST['deleteprofileconfirmation'])) {
//delete action
} else if (isset($_POST['submit'])) {
//submit action
} else {
//no button pressed
}
I want to create a checkbox and check whether checkbox is checked or not in another PHP page so I created form like :
<form action="heartbeat.php" method="post">
<input type="checkbox" name="keepme">
<input type="submit" value="log in">
</form>
and then I tried to extract this input on heartbeat.php like:
<?php
/*
* A PHP file for laying down a heartbeat JavaScript call.
*/
if(!isset($_POST["keepme"])){
$auto_logout = 10;
}
else{
$auto_logout = 1000;
}
?>
but it never gets $_POST["keepme"] value. Any idea??
If you don't pass value to checkbox tag, the POST array will be empty. You need to do something like that
<input type="checkbox" name="keepme" value='1'>
Make Sure you have submit button
If(isset($_POST['submit-btn'])){
echo $_POST['keepme'];
}
As the title says This is the code that I tried with. The forms must appear one by one because information from previous forms determine how the next ones will look.
$(document).ready(function(){
$('#first_form').submit(function(){
$('#first_form').fadeOut('fast');
$('#second_form').fadeIn('fast');
});
});
<form action="new_patch.php" method="POST" id="first_form">
Title: <input type="text" name="patch" placeholder="Patch 4.20">
<br/>
Number of Champions: <input type="number" name="champ_number" min="1" max="99">
<br/>
<input type="submit" value="submit">
</form>
<form action="new_patch.php" method="POST" id="second_form" style="display: none;" >
<input type="text" value="text">
<input type="submit" value="submit">
<?php
$champ_number = null;
if(isset($_POST['champ_number']))
{
$champ_number = $_POST['champ_number'];
for($champ_number;$champ_number>0;$champ_number--)
{
echo "<br/>Champion ".$champ_number."<input type=\"number\" name=".$champ_number." min=\"1\" max=\"99\">";
}
}
?>
</form>
You're mixing client-side and server-side form code. Submitting the form will reload the page entirely, so from the looks of your code it will fade in the new form when the old form is submitted, but then reload the page so the old form will show again anyway.
You could either:
Let the PHP determine how the next form appears based on the submission of the first form, e.g. if (isset($_POST["First_form_submit"]) { Show second form... }
Probably better and more user-friendly: make the second form appear below once the user has filled in the relevant inputs on the first form before they've submitted
you can use:
$('#first_form').submit(function(){
$('#first_form').fadeOut(function() {
$('#second_form').fadeIn('fast');
});
return false;
});
From the jQuery documentation the syntax is fadeIn( [duration ] [, complete ] ) it accepts a duration and a onComplete callback that you can use to execute the next action when the first is completed.
I did this once too, just add a submit class to the button and make it like this:
<input type="submit" value="submit" class="submit">
Change script to a click function.
$(document).ready(function(event){
event.preventDefault();
$('.submit').click(function(){
$('#first_form').fadeOut(400);
$('#second_form').fadeIn(400);
});
});
PS, also you need to prevent submit default...otherwise it will just submit the form, see this JSfiddle
I have a button on my form. I want to check if it's pushed or not. I tried like this:
<input type="button" name="btn" id="btn" class="btn1" value="Creaza Sondaj" />
if (isset($_POST['btn']))
But this method is only working for submit buttons. How can I check if a button is pushed?
If you want to handle pushing button with PHP, change it's type to submit.
Otherwise handle click event with JavaScript.
What you might want to do is when a button is pressed, add a hidden input field to your form with javascript. That hidden field won't be displayed to the user, but once the form is submitted, your PHP will be able to read the value of that input. If the value exists, the button was pressed.
Here is a jQuery example -
$('#btn').on('click',function(){
$('#form').append('<input type="hidden" name="form_data[btn]" value="btn_clicked!" />');
});
Here is a "raw" JavaScript example, execute this code with an onclick event -
var newInput = document.createElement('input');
newInput.innerHTML = '<input type="hidden" name="form_data[btn]" value="btn_clicked!" />';
document.getElementById('form_id').appendChild(newInput);
Make sure to give it a name attribute so that you'll know where to look for the values in your PHP.
You have to use $_POST i.e. make your form submit to the page with:
<?php
if (isset($_POST['btn']))
Otherwise, you can use JavaScript/jQuery like:
<input type="button" name="btn" id="btn" class="btn1" value="Creaza Sondaj" />
<script>
$('#btn').click(function(){
// do something
});
</script>