Modifying $i inside a form - php

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>

Related

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

Saving info while submitting page with GET in PHP

<form method='get' action='y.php'>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
if (isset($_GET['btnSave'])) {
$name=isset(($_GET['txtName'])?isset($_GET['txtName']:'');
//then Logic of insert goes here
}
}
?>
so before moving to y.php the record must be saved.
but I cant get the $name value, as action given to y.php.
How can I get $name which contain value in text box.
if you change the action to this (same/current) page record is going to database without any flaw or error.
try using post method instead and change your code accordingly, try this:
<form method='post' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit' name='submit'/>
</div>
</form>
<?php
if (isset($_POST['submit'])) {
$name=$_POST['txtName'];
//then Logic of insert goes here
//redirect to y.php with name value
echo "<script>window.open('y.php?user=$name','_self')</script>";
}
?>
Then use $nme = $_GET['user']; to get the value of $name in y.php
Try this code,
<form method='get' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
if (isset($_GET['txtName'])) {
$name=$_GET['txtName'];
//then Logic of insert goes here
}
}
?>
I suggest you use method post. You can code like this
<form method='post' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if(isset($_POST['txtName'])){
$name =$_POST['txtName'];
echo $name;
}
?>
you can use the session if you want make the value use in another file.
I hope that can solve your problem

PHP Like Button Problems

I am trying to make a like button for posts on my website.
PHP for like query (d_db_update is
function d_db_update($string) {
return mysql_query($string);
}
)
if($_GET['like']) {
$like = d_db_update("UPDATE posts set post_rating = post_rating+1 WHERE post_id = {$_GET['like']}");
}
Button
<form action='{$_SERVER['PHP_SELF']}&like={$posts_row['post_id']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='submit' name='like' value='Like' /></p>
</form>
What can I do to fix it/make it work?
Use below form with a hidden input it solve your problem.
<form action='{$_SERVER['PHP_SELF']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='hidden' name='like' value='{$posts_row['post_id']}' />
<input type='submit' value='Like' /></p>
</form>
You are using your form action wrong.. if you are using get method than there is not need to use the form..
try this..
<a href='yourpage.php?like=<?php echo $post_id ?>'>Like</a>
your submit button name and like variable which you have used in action url are the same , and you used get method in method of form.So, you need to change the submit button name.
or
you can do it without using form only on button click try below code
<input type='button' name='like' value='Like' onclick="location.href='yourpage.php?like=<?php echo $post_id ?>'" />
Change your code to this
You can not write PHP variables using {}. You need to echo them out.
<form action='' method='get'>
<p align='left'><?php echo $posts_row['post_rating'] ?>
<input type='hidden' name='like' value='<?php echo $posts_row["post_id"] ?>' />
<input type='submit' value='Like' /></p>
</form>
Edit--
You were not returning the post id correctly, I made the changes, also there is no need to provide any action as it will be self only.

Having difficulty with form data

I have 3 textfields and only one submit button to send all the data of the textfield at once. But how do I send the data of all three textfields at once in php?
<form>
for($x=0;$x<3;$x++){
<input type="text" name="name">
}
<input type="submit" name="submit">
</form>
Now I have a three fields inside a for loop and I have to extract data from all of them using single submit button.So how can I do that?
Using the 'name' attribute on an input allows you to do this for example
<form action='submit.php' method='post'>
<input type='text' name='one'></input>
<input type='text' name='two'></input>
<input type='text' name='three'></input>
<input type='submit' name='submit' value='Submit!' />
</form>
and in your PHP you would do something like this
<?php
if(isset($_POST['submit'])){
$inputOne = $_POST['one'];
$inputTwo = $_POST['two'];
$inputThree = $_POST['three'];
//Do whatever you want with them
}
?>
There are better ways of doing this, but this is probably the simplest to understand
If you want all the inputs to have the same name do this
<input type='text' name='textinput[]'></input>
Use that instead and loop through all of the inputs like so
<?php
foreach($_POST['textinput'] as $input){
//do something with $input
}
?>
You put them all inside the same <form> and make sure they have different values for the name attributes (or that the values end in []).
I believe what you are looking for is this Note the [ ] behind the name field
<form>
for($x=0;$x<3;$x++) {
<input type="text" name="name[]" />
}
<input type="submit" name="submit" />
</form>
Then to retrieve the values
$names = $_POST['name'];
foreach( $names as $name ) {
print $name;
}

How to get selected radio type input and use it as a variable?

How can i get the selected radio input type value in a php script and direct it to a different directory to retrieve the files selected. I tried writing the code, but couldn't write it properly since i m a newbie in the world of php.
The code i have written for displaying radio button is
<input type="radio" name="radio<?php echo $orderby1[0]; ?>"
id="<?php echo $orderby1[0]; ?>" value="<?php echo $orderby1[0]; ?>"
/><?php echo $orderby1[0]; ?>
Now how to get the selected values from it and direct it to the directory contains the file?
The code i have written is , which doesn't seems to work
<script type="text/javascript">
var element = document.getElementById("<?php echo $orderby1[0]; ?>");
jmolApplet(400, "load /jmol/'element.value'.txt");
</script>
I don't want to include any for loops... that makes my script work differently.
Please help!
You would use a $_REQUEST method within a form, something like:
<?php
if ($_REQUEST['radiotest']) {
echo "You chose ".$_REQUEST['radiotest'];
}
?>
<form>
<div><input type='radio' name='radiotest' value='1' /> Option 1</div>
<div><input type='radio' name='radiotest' value='2' /> Option 2</div>
<div><input type='radio' name='radiotest' value='3' /> Option 3</div>
<div><input type='submit' value='Submit' /></div>
</form>
You need first to execute when the radio has rendered and then not put the value inside the quotes
function someEvent() {
var element = document.getElementById("<?php echo $orderby1[0]; ?>");
jmolApplet(400, "load /jmol/"+element.value+".txt");
}

Categories