How to retrieve textbox data in php - php

I want to retrieve the data from a textbox which I created. Please take look at my code and help me.
<?php
if(isset($_GET['ok']))
{
$a=1;
$n=$_GET['n'];
for($i=0;$i<$n;$i++){
echo '<form action="exa.php" method="get">';
echo '<input type="text" name="kal'.$a.'"/> <br/>';
echo '</form>';
$a++;} $a=1;
for($i=0;$i<$n;$i++)
{
$txtnm="kal".$a;
$kal=$_GET['$txtnm'];
echo $kal;
$a++;
}
}
?>
<html>
<body>
<form action="exa.php" method="get">
<input type="text" name="n"/><br/>
<input type="submit" value="OK" name="ok"/>
</form>
</body>
</html>
Here I am getting an error saying 'undefined index $txtnm'

here i am getting error that undefined index $txtnm ...
$kal=$_GET[$txtnm];
remove the single quotes, you are treating it as constant if you put those single quotes

Try to do like this
echo 'Input '.$a.'<input type="text" name="kal[]"/> <br/>';
and after submitting form the kal array like
print_r($_REQUEST['kal']);
And why you are using get method,without specific need dont use get method while submitting the textarea data,because some of special characters causes your redirect

Remove single quote from $kal=$_GET['$txtnm'] . It should be
$kal=$_GET[$txtnm];

Related

confused about basic find and replace app

I am trying to do a find and replace applicaiton the problem is that after cliked submit button all the text fields gets clean nothing displays on the screen What am i doing wrong
<?php
$offset=0;
if(isset($_POST['text'] ) && isset($_POST['searchfor']) && isset($_POST['replacewith'])){
$text=$_POST['text'];
$search=$_POST['searchfor'];
$replace=$_POST['replacewith'];
$searchLength=strlen($search);
if(!empty($text) && !empty($search) &&!empty($replace)){
while ($strpos= strpos($text,$search,$offset)){
echo $offset=$strpos+$searchLength;
}
} else {
echo "<script>alert('errorrrr')</script>";
}
}
?>
<form action="#" method="post">
<textarea name="text" id="" cols="30" rows="10"></textarea><br>
Search For:<br>
<input type="text" name="searchfor"><br>
ReplaceWith<br>
<input type="text"name="replacewith"><br>
<input type="submit" value="Fr..."></>
</form>
Regarding your form, you decided to submit to the same page.
Doing this, the page is obviously fully reloaded when submitted. Hence it is normal that what you typed in has disappeared.
If you want to see it again, you have to display you variables in the HTML code.
For example:
<?php
$myVar = "";
if(isset($_POST['myVar']){
$myVar = $_POST['myVar'];
}
?>
<form>
<input type="text" value="<?php echo $myVar;?>"/>
</form>
NB: I encourage you to filter the user entry.
Regards
there is problems in your code :
1 - echo $offset=$strpos+$searchLength; the echo can't be used in this format. insted use echo $offset; in next line for seeing offset values.
2 - if the text be like 'amir love persepolis' and search for 'amir' to replace it with 'all men's' you will have another issue, because you will have while ( 0 ) situation. think about this too!

Changing value of $_SESSION variable using submit button

I am trying to make code changing news pages and I am having difficulties.
$_SESSION['page'] doesn't change value and always stays as 1.
Thank you.
<?php
session_start();
if (!isset($_POST['set_page'])) {
$_SESSION['page'] = 1;
}
else {
eval("return '".$_POST['change_page']."';");
}
echo "Page ".$_SESSION['page'];
echo '<form action="test.php" method="post">';
echo '<input type="hidden" name="change_page" value="$_SESSION["page"]++"/>';
echo '<input type="submit" name="set_page" value="Next Page"></form></p>';
?>
Your statement here is wrong ++ operator will work like that change that line like this
echo '<input type="hidden" name="change_page" value="'.$_SESSION["page"]++.'"/>';
and your single quotes, When you embed some variable inside string use double quotes or use concatenation.

PHP self-form variable?

I was wondering if it is possible to use a variable from a form as its own URL... its hard to explain in my opinion, heres an example:
matchmaking.php:
$search_summoner = $_POST['search_summoner'];
echo '<form method="post" action="matchmaking.php?search=' . $search_summoner . '">';
echo '<input type="text" name="search_summoner">';
echo '<input type="submit">';
echo '</form>';
As you can see the action sends it back to matchmaking.php but with a variable from the form which was just submitted. The code above, which I have tried, didn't seem to work; So I wondered if anyone else had any ideas on how to do this...
Thanks for the help in advance
using jquery you could set the action parameter like this (add an ID "search" to the form)
$('input[name="search_summoner"]').on('keyup'function(){
$('form#search').attr('action','matchmaking.php?search='+$(this).val());
});
So, while typing, the action parameter will be extended by every character of the typed search string.
Try this;
<?php
if(isset($_POST['search_summoner']) && $_POST['search_summoner'] != '')
{
$search_summoner = $_POST['search_summoner'];
}
else
{
$search_summoner = "";
}
echo $search_summoner; // This displays your Query
?>
<html>
<body>
<form method="post" action=<?php echo $_SERVER['PHP_SELF']; ?>>
<input type="text" name="search_summoner">
<input type="submit">
</form>
</body>
</html>
If I understand you right, I think you want to do something like this:
$search_summoner = $_POST['search_summoner'];
echo '<form method="post" action="matchmaking.php">';
echo '<input type="hidden" name="search" value="'.$search_summoner.'"/>';
echo '<input type="text" name="search_summoner">';
echo '<input type="submit">';
echo '</form>';

PHP print and echo not working

I am new to PHP and I was making this form and I wanted to print some data but it is not displaying. What is wrong with it? Here's the code:
<form name="input" action="check.php" method="get">
Unit number:
<input type="number" name="unit" />
<input type="submit" value="Submit" />
</form>
<table>
<tr><td class="check-table">
<?php
if($_GET[unit] = null) $output="<p>Please Enter A Unit Number</p>";
echo $output;
?>
</td></tr></table>
Please Help?
The better way would be:
if (empty($_GET['unit'])) {
$output="<p>Please Enter A Unit Number</p>";
echo $output;
}
The reasons:
You check if variable exists
You use ' quotes for array key name
You output $output variable only if it is necessary. And in your case - you output it even if it doesn't exist
You've also confused == (comparison operator) and = (assignment operator)
I think you missed the single quotes in the $_GET['unit']
<?php
if($_GET['unit'] = null) $output="<p>Please Enter A Unit Number</p>";
echo $output;
?>

PHP List of records from filemaker select button

First, thank you for reading this.
I am just starting php and I am tying to make a site using FileMaker to display and enter information.
I have the php connecting to my database, then a search page using a form, then it displays a list of records. I would like to make a "button" that will select one record then display related records.
This is where my trouble is. I do not know how to make a form that will save either the record_Id or key field to then display the next page.
I am using a foreach loop to display the list in a table:
$records = $result->getRecords();
echo '<table border="1">';
echo '<tr>';
echo '<th>Company</th>';
echo '<th>Id Num</th>';
echo '<th>Choose</th>';
echo '</tr>';
foreach ($records as $record) {
echo '<tr>';
echo '<td>'.$record->getField('Company').'</td>';
echo '<td>'.$record->getField('K_Medical').'</td>';
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='$record->getField('K_Medical')/>';
<input type="submit" />
</form>';
echo '</form></td>';
echo '</tr>';
}
echo '</table>';
As you can see I have tried to use a hidden form field to get the key field of the record, but the page dose not work. I get an error 500 when I try to view it in a browser.
Any help would be greatly appreciated! If I have not provided enough information please let me know.
Replace :
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='$record->getField('K_Medical')/>';
<input type="submit" />
</form>';
By :
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='.$record->getField('K_Medical').'/>
<input type="submit" />
</form>';
You have a quotes and concatenation errors.

Categories