php post not able to grap element which append by using jquery - php

here is my code, hope you all can understand.
[html form coding]
<form method='post' action='pro/handle.php'><br/>
<input type='text' name='FirstTXT' value='First'>
<button type='button' name='ADDINPUT'>add</button>
<div id='SETSET'>
<!-- prepare for adding input element by using jquery -->
</div>
<input type='text' name='LastTXT' value='Last'>
<button type='submit' name='BTN' value='Submit'>submit</button>
</form>
[Jquery coding]
$(document).on("click","[name=ADDINPUT]",function(){
var inp = "<input type='text' name='ADDTXT'>";
$("#SETSET").append(inp);
});
So far NO error/problem for the code above(tested), jquery input did append into #SETSET accordingly.[The problem is]
after click on form [submit] button, php NOT able to get the element of [ADDTXT]when i use php to loop & get everything from previous page, result as below:-
[PHP coding]
foreach($_post as $key=>$val){
echo "<br/>$key : $val";
}
[Result]
FirstTXT : First<br/>
LastTXT : Last<br/>
BTN : Submit<br/>
//=== Missing ADDTXT (between First & Last)===//
Did you guys/gals know why?

Maybe that:
html:
<form method='post' action='pro/handle.php'><br/>
<input type='text' name='FirstTXT' value='First'>
<button type='button' name='ADDINPUT'>add</button>
<div id='SETSET' style='display: none;'>
<!-- prepare for adding input element by using jquery -->
<input type='text' id='ADDTXT' name='ADDTXT' disabled='disabled'>
</div>
<input type='text' name='LastTXT' value='Last'>
<button type='submit' name='BTN' value='Submit'>submit</button>
</form>
and JS:
$(document).on("click","[name=ADDINPUT]",function(){
$("#SETSET").show();
$("#ADDTXT").removeAttr("disabled");
});

You have an array from that field.
Replace:
var inp = "<input type='text' name='ADDTXT'>";
With:
var inp = "<input type='text' name='ADDTXT[]'>";
And PHP:
if(is_array($val)) { print_r($val); }
OR try:
var no = 0;
$(document).on("click","[name=ADDINPUT]",function(){
++no;
var inp = "<input type='text' name='ADDTXT"+ no +"'>";
$("#SETSET").append(inp);
});

Related

Is it possible to have multiple actions in forms using only one submit button?

See question -
I've tried this:
echo "<form action='index.php' method='post'>
Use the previous input of participants: <input type='radio' name='participants[]'value='$participants[0]'>
OR <br>
<form action='index.html' method='post'>
Use a different input of participants: <input type='radio' name='participants[]' value='0'>
<input type='submit' value='send' name='send'> </form> <br>";
Both of the radio button lead me to index.php when I want to be able to go to index.html in case i press on the second radio button.
You may solve it by using JQuery
<form action='index.php' method='post'>
Use the previous input of participants:
<input type='radio' name='participants[]' value='$participants[0]'>
<br/>OR <br>
Use a different input of participants:
<input type='radio' name='participants[]' value='0'>
<input type='submit' value='send' name='send'>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var form = $('form');
var input = $('input[type=radio]');
input.on('click', function() {
if ($(this).val()==0) {
form.prop('action', 'index.html');
} else {
form.prop('action', 'index.php');
}
});
});
</script>

PHP: Multiple forms aren't working except the first one?

This code generates multiple form associated with each product and its id.
But at html side only first is working. When I inspected the page I came to know that this code only generates form for first product only. Anyone Else faces this?
for ( $b = 0; $b < sizeof( $id ); $b++ ) {
echo "
<form action='Post.php' method='GET'>
<div class='form-group' style='display:none' id='$id[$b]'>
<label class='control-label'>Message</label>
<input type='text' name='id' value='$id[$b]'style='display:none'>
<input type='text' name='nam' value='admin 'style='display:none'>
<textarea type='text' class='form-control ' rows='4' col='10' name='mess' >
</textarea>
<input style='margin-top:10px' type='submit' class='btn btn-info' value='Submit'>
</div>
</from>";
}
You seem to close your form tag with 'from'.
change /from to /form

insert and delete functionality with ajax

I am working on basic functionality of insert,add,delete and update using ajax.
I have done little functionality write now..actually i want all functionality in a grid .
I put all html code inside a for loop.. this loop will execute till total num rows.. first execution of loop is fine, problem occur after this... next time when i click on delete button ajax call is not working.
Need help on this....
I have 2 files.. form.php and operation.php
code of form.php..
$(document).ready(function(){
$("#delete").click(function(){
var id=$("#uid").val();
$.post("operation.php",{ID:id},function(data){
$("#result").html(data);
});
});
});
for($i=0;$i<2;$i++)
{
echo "<input type='text' value=".mysql_result($all_records, $i, "id")." name='uid' id='uid'>
<input type='text' value=".mysql_result($all_records, $i,"name")." name='name' id='email' placeholder='Email'>
<input type='text' value=".mysql_result($all_records, $i,"email")." name='email' id='email' placeholder='Email'>
<input type='password' value=".mysql_result($all_records, $i,"password")." name='pass' id='pass' placeholder='Password'>
<input type='submit'name='delete' id='delete' value='Delete'>
<input type='submit'name='update' id='update' value='Update'>";
echo "</td></tr>";
}
operation.php
write now i put only one line code for checking purpose...
echo $_POST['ID'];
Well i think you know that ids should have unique names. It is not a good idea to give same id to multiple elements in the same page.
I think the code bellow will solve your purpose.
$(document).ready(function(){
$(".delete").click(function(){
var id=$(this).data('id');
$.post("operation.php",{ID:id},function(data){
$("#result").html(data);
});
});
});
for($i=0; $i<2; $i++)
{
echo "<input type='text' value='".mysql_result($all_records, $i, "id")."' name='uid' class='uid'>
<input type='text' value='".mysql_result($all_records, $i,"name")."' name='name' class='name' placeholder='Email'>
<input type='text' value='".mysql_result($all_records, $i,"email")."' name='email' class='email' placeholder='Email'>
<input type='password' value='".mysql_result($all_records, $i,"password")."' name='pass' class='pass' placeholder='Password'>
<input type='button'name='delete' data-id='".mysql_result($all_records, $i, "id")."' class='delete' value='Delete'>
<input type='submit' name='update' class='update' value='Update'>";
echo "</td></tr>";
}
Try to understand it and compare it with your previous code.
Let me know if you need further assistance.

Ajax form onsubmit stop refresh

I have one page lets call it "X.php" that i use that calls another page "Y.php" with the xmlhttp.open() function.
At the moment i have a form in page Y.php, and i would like to prevent it from refreshing because once i click on the submit button, my form dissapear.
I tryed with Jquery 'event.preventDefault()' in page X.php but nevertheless it didnt work.
Does anyone know a solution?
while($row = mysql_fetch_array($query)) {
echo " <h2>Update your personal settings</h2>
<form action='' id='target' method='POST'>
Surname: <input type=text name='lid_voornaam' value='".$row['lid_voornaam']."'><br/>
Name: <input type=text name='lid_naam' value='".$row['lid_naam']."'><br/>
Email: <input type=text name='lid_email' value='".$row['lid_email']."'><br/>
<input type='submit' value='Whatever' id='test1'/>
</form>";
}
Java script:
<script type="text/javascript">
$(document).ready(function(){
$("#test1").click(function(event){
event.preventDefault();
});
});
In your form tag. Add onSubmit="return false;
<form action='' id='target' method='POST' onSubmit="return false;>
Surname: <input type=text name='lid_voornaam' value='".$row['lid_voornaam']."'><br/>
Name: <input type=text name='lid_naam' value='".$row['lid_naam']."'><br/>
Email: <input type=text name='lid_email' value='".$row['lid_email']."'><br/>
<input type='submit' value='Whatever' id='test1'/>
</form>
As #andrew mentions. Your php script will generate multiple <form> tags which will all have the same id id='target'. This can cause problems on your page as that isnt valid html
I would have thought that the javascript you posted should work actually.
But if this is your actual code I suspect it fails because all of the submit buttons on all forms have id='test1' and id should be unique
Try like this:
$i=0;
while($row = mysql_fetch_array($query)) {
echo " <h2>Update your personal settings</h2>
<form action='' id='target."$i++".' method='POST'>
Surname: <input type=text name='lid_voornaam' value='".$row['lid_voornaam']."'><br/>
Name: <input type=text name='lid_naam' value='".$row['lid_naam']."'><br/>
Email: <input type=text name='lid_email' value='".$row['lid_email']."'><br/>
<input type='submit' value='Whatever' id='test1'/>
</form>";
}
and
$("form").submit(function(event){
var id = $(this).attr('id').split('target')[1];
alert('form ' +id+ ' clicked');
event.preventDefault();
});

Modifying $i inside a form

so I have a form. The form consists of 10 lines by default. It goes like this:
<form method="post" action="actionhere">
<?php
for($i=0; $i<10;$i++) {
?>
<div class='clone_me'>
<span>Line <?php echo $i;?></span>
<input type='checkbox' name='ck_<?php echo $i;?>'/>
<input type='text' name='tx_<?php echo $i;?>'/>
</div>
<?php } ?>
<input type='submit' name='submit' value='Submit'/>
</form>
So inside the form, we will have 10 rows of checkbox+textbox.
What I'm trying to make is, I want to place a button to add new row (the checkbox+textbox). Now, problem is, I need the $i value (since it's form the for loop). Is that possible that when we click the add row button, the value of $i that we set inside for loop be incremented by 1 on each click? I know we can clone the div using jquery, but how about the $i value?
I think you are doing it in wrong way you do not need $i value inside name attribute you have to use array for it for example
<form method="post" action="test.php">
<div class='clone_me'>
<span>Line 1</span>
<input type='checkbox' name='ck[]'/><!--this field should menditory-->
<input type='text' name='tx[]'/>
<span>Line 2</span>
<input type='checkbox' name='ck[]'/><!--this field should menditory-->
<input type='text' name='tx[]'/>
</div>
<input type='submit' name='submit' value='Submit'/>
</form>
Now implement this code in actionhere.php
<?php
$cks = $_POST['ck'];
$txs = $_POST['tx'];
foreach($cks as $key => $ck) {
echo $ck."<br>";
echo $txs[$key]."<br>";
}
?>
Well, basically no. Your PHP script is already over when the html has been generated. So you can't rely anymore on PHP. But you don't need to make it explicitly appear in your html.
You should count the rows using jquery :
var i = $('form').find('.clone_me').length
and then add a new row using javascript again :
$('form .clone_me:last').clone().insertAfter('form .clone_me:last');
<input type='hidden' name='counter' id='counter'/>
<input type='checkbox' name='chk' id='chk'/>
<?php
$counter=$_POST['counter'];
for($i=0;$i<=$counter;$i++)
{
$chk=$_POST['chk'.$i];
// Your Insert Code Here
}
?>
may be this can be but have some limit
if you have no problem with page reload the you can do it is:-
by this way your page will reload and the value in textbox and checkbox will gone:---:)
every time new page generate and send by server to client browser
<?php
if(isset($_POST['submit'])){
$ends = $_POST['ttl_rows'];
}else{
$ends = 10;
}
?>
<form method="post" action="#">
<?php
for($i=1 ; $i<$ends;$i++) {
?>
<div class='clone_me'>
<span>Line <?php echo $i;?></span>
<input type='checkbox' name='ck_<?php echo $i;?>'/>
<input type='text' name='tx_<?php echo $i;?>'/>
</div>
<?php } ?>
<input type='hidden' name='ttl_rows' value='<?php echo ($i+1); ?>'/>
<input type='submit' name='submit' value='Submit'/>
</form>

Categories