I am trying to create a program that prints off an array of five user-inputted elements. Each input box should take in a value from the user, store it in an array, and print off that array value. However, the values reset and become null when you enter text into the next input box. You can see what I mean by looking at the site.
https://people.emich.edu/ghaines1/cosc231/stringReverse.php
I eventually want to print off the values of the strings in reverse as well. I'm sure that's quite easy but I haven't looked into it yet, as I need this first part to work first.
So ideally, the PHP portion should print off:
$input[0]
$input[1]
$input[2]
$input[3]
$input[4]
assuming that it has retained those values. Again, my issue is that I cannot print more than one value because they reset and become null when I enter a value into the next text box. Here's the code:
<form action="stringReverse.php" method = "post">
<input name="st1" type="text" size="50" placeholder="Enter phrase"><br><br>
</form>
<form action="stringReverse.php" method = "post">
<input name="st2" type="text" size="50" placeholder="Enter phrase"><br><br>
</form>
<form action="stringReverse.php" method = "post">
<input name="st3" type="text" size="50" placeholder="Enter phrase"><br><br>
</form>
<form action="stringReverse.php" method = "post">
<input name="st4" type="text" size="50" placeholder="Enter phrase"><br><br>
</form>
<form action="stringReverse.php" method = "post">
<input name="st5" type="text" size="50" placeholder="Enter phrase"><br><br>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$input = array (0, 0, 0, 0, 0);
if (is_null($_POST["st1"]) == false) {
$input[0] = $_POST["st1"];
echo $input[0] . "<br>";
}
if (is_null($_POST["st2"]) == false) {
$input[1] = $_POST["st2"];
echo $input[1] . "<br>";
}
if (is_null($_POST["st3"]) == false) {
$input[2] = $_POST["st3"];
echo $input[2] . "<br>";
}
if (is_null($_POST["st4"]) == false) {
$input[3] = $_POST["st4"];
echo $input[3] . "<br>";
}
if (is_null($_POST["st5"]) == false) {
$input[4] = $_POST["st5"];
echo $input[4] . "<br>";
}
}
?>
its because you are defining a form for each input
change html to:
<form action="stringReverse.php" method = "post">
<input name="st1" type="text" size="50" placeholder="Enter phrase"><br><br>
<input name="st2" type="text" size="50" placeholder="Enter phrase"><br><br>
<input name="st3" type="text" size="50" placeholder="Enter phrase"><br><br>
<input name="st4" type="text" size="50" placeholder="Enter phrase"><br><br>
<input name="st5" type="text" size="50" placeholder="Enter phrase"><br><br>
</form>
Related
I want to see all field input and textarea but I don't know how to
show it together in the same code.
and in case I want to skip something for example I want to write
directly input name without make him check the type or I don't want him to show me the readonly so how can I do it ?
<form method="post" action="" id="submit_form">
<input type="text" name="TITLE" value="" size="40" maxlength="100" class="text" />
<textarea name="DESCRIPTION" rows="4" cols="36" class="textarea"></textarea>
<input type="text" name="DESCRIPTION_limit" size="4" class="limit_field" readonly="readonly" value="250" />
<textarea name="ARTICLE" id="ARTICLE" rows="6" cols="50" class="textarea"></textarea>
<input type="text" name="META_KEYWORDS" value="" size="40" maxlength="2000" class="text" />
preg_match_all('/<(input)[\s](type)="?([^>"]*)"?[\s](name)="?([^>"]*)"?[\s]/', file_get_contents($url), $matches);
echo"<pre>";
print_r($matches);
I tested this code with the line "$string". Type and Name attributes can be in any order in the input tag, and it searches until it reads a ">" character.
$string = '<input type="text" name="TITLE" value="" size="40" maxlength="100" class="text" />';
preg_match_all('/<input[^>]*?(?:(type)="([^"]*)")|(?:(name)="([^"]*)")/', $string, $matches);
if($matches[1][0] == "type" && $matches[3][1] == "name"){
$type = $matches[2][0];
$name = $matches[4][1];
}
elseif($matches[1][0] == "name" && $matches[3][1] == "type"){
$name = $matches[2][0];
$type = $matches[4][1];
}
else{
throw new Exception('no input tags with type and name attributes were found!');
}
echo $type;
echo $name;
I have got my validation working but I wanted to only output it, when it has been submitted but isset nor empty seems to work for me for some reason? I am using php 5.5.3 I believe
My code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="register">
<input type="text" placeholder="Username" maxlength="15" name="username"/><br>
<input type="password" maxlength="15" name="password1"/><br>
<input type="password" maxlength="15" name="password2" /><br>
<input type="text" placeholder="your#email.com" maxlength="25" name="email"/><br>
<input type="text" maxlength="20" name="county" /><br>
<input type="submit" value="Register" name "register"/>
</form>
<?php
//Form Validation
if(empty($_POST['register']))
{
echo "Option A <br>";
}
else
{
echo "Option B <br>";
}
Also, I can't remember how I would enter in the same information for that user if it validates fine?
value="<?php $_POST['stuff'] ?>
<input type="submit" value="Register" name "register"/>
^
You're missing an = there.
It should be:
<input type="submit" value="Register" name="register"/>
It's betterto use isset() instead of empty(), in this case:
if(isset($_POST['register']))
{
echo "Option A <br>";
}
else
{
echo "Option B <br>";
}
I'm not sure what Option A and B are, but with this code, you'll always see Option B when you load the page. The condition in your if block will execute to FALSE and hence the else block will executed. If you want to avoid this, you can use an elseif statement instead of an else.
I have had a look at a few answers but none relates to my specific problem. I have an array that takes user input and sorts the array in an ascending order. That's all good, I now want the user to enter a number for which I can locate the array index, if the number is not located, the function returns -1. I cannot put my finger on the error, I'm getting -1 at all times.
Any help is truly appreciated
This is my form that picks up the information.
<form action="welcome.php" method="POST" value="">
Name: <input type="text" name="fname" value="">
Age: <input type="text" name="age" value="">
Integer :<input type="number" name="integer" value="">
<div class="container">
<p> Please choose numbers: </p>
<label>First:</label>
<input type="number" name="name[]" ><br />
<label>2nd</label>
<input type="number" name="name[]"><br />
<label>3rd</label>
<input type="number" name="name[]" ><br />
<label>4th</label>
<input type="number" name="name[]"><br />
<label>5th</label>
<input type="number" name="name[]"><br />
<label>6th</label>
<input type="number" name="name[]"><br />
<label>7th</label>
<input type="number" name="name[]"><br />
<label>8th</label>
<input type="number" name="name[]"><br />
<label>9th</label>
<input type="number" name="name[]"><br />
<label>10th</label>
<input type="number" name="name[]"><br />
<label>Pos Number</label>
<input type="number" name="pos"><br />
<input type="submit">
And the index locator:
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.
<?php
$tmp = trim($_POST['integer']);
if (!ctype_digit($tmp)) {echo "Input requires an inetger " ;}
else{
echo "The integer is ".$_POST["integer"]."<br>";
}
echo"You have entered the numbers below <br>";
$name=$_POST['name'];
sort($name);
foreach( $name as $v) {
print $v."<br>";
}
echo "You wish to find the position of ".$_POST['pos']."<br />";
function search($name,$K){
$l=0;
$size=sizeof($name);
$u=$size-1;
$m=0;
while ($l<=$u){
$m = ($l+$u)/2;
round($m);
echo "this is m ".$m;
if ($name[$m]<$K) {
$l=$m+1;
}
else if ($name[$m]==$K) {
return $m;
return $m;
} else {
$u=$m-1;
}
}
return -1;
}
$po=$_POST['pos'];
$b=search($name,$po);
echo "The position of the entered number is".$b;
?>
I have 2 PHP pages...
INDEX.PHP
with this code:
<form method="post" action="" name="f1">
<input type="text" name='p_name' size='50'><br>
<input type="text" name='p_name2' size='50'><br>
<a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("index2.php","Ratting","width=550,height=170,left=150,top=200,toolbar=1,status=1,");>Click here to open the child window</a>
</form>
and INDEX2.PHP
with this avascript code:
<script langauge="javascript">
function post_value()
{
opener.document.f1.p_name.value = document.frm.c_name.value;
opener.document.f1.p_name2.value = document.frm.c_name2.value;
self.close();
}
</script>
and this PHP/HTML:
<form name="frm" method="post" action="">
<?php
$sql="SELECT * from customer";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
echo '<input type="text" name="c_name" size="50" value="'.$result["sequence"].'" /><br>
<input type="text" name="c_name2" size="50" value="'.$result["company"].'" /><br>
<input type=button value=\'Submit\' onclick=\'post_value();\'><br><br>';
}
?>
</form>
basically, when you go to index.php, you click the link to open the popup window index2.php and that then lists customers from a database and gives each one 2 text boxes - one for the sequence/id of the customer and the other for the company name and one submit button per row.
When the button is pressed, it runs the javascript function post_value(); which puts the values from the database/child popup window (index2.php) into the text boxes in the parent window (index.php)
when i run this code, it just puts the word undefined in box boxes on the parent page, however if i remove the while loop in php and it just displays the one row from the database of customers it works fine.
its like it doesn't like the while loop in php but i cannot work out why.
any help would be much appreciated.
Edit - I just realized that your while loop is going to generate something that looks like:
<input type="text" name="c_name" size="50" value="A1" /><br>
<input type="text" name="c_name2" size="50" value="B1" /><br>
<input type=button value=\'Submit\' onclick=\'post_value();\'><br><br>
<input type="text" name="c_name" size="50" value="A2" /><br>
<input type="text" name="c_name2" size="50" value="B2" /><br>
<input type=button value=\'Submit\' onclick=\'post_value();\'><br><br>
<input type="text" name="c_name" size="50" value="A3" /><br>
<input type="text" name="c_name2" size="50" value="B3" /><br>
<input type=button value=\'Submit\' onclick=\'post_value();\'><br><br>
This isn't going to work because you have multiple elements named "c_name" and "c_name2" within the same form. I think what you want to do is put your form element inside the while loop like this:
<?php
$sql="SELECT * from customer";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$ctr = 0;
while($result=mysql_fetch_array($rs))
{
echo '<form name="frm' . $ctr . '" method="post" action="">
<input type="text" name="c_name" size="50" value="'.$result["sequence"].'" /><br>
<input type="text" name="c_name2" size="50" value="'.$result["company"].'" /><br>
<input type=button value=\'Submit\' onclick=\'post_value(' . $ctr . ');\'><br><br>'
</form>';
$ctr++;
}
?>
Notice how I created a $ctr variable so you can identify each form and how it is passed into post_value. You'll need to use that to grab the correct form like this:
<script langauge="javascript">
function post_value(ctr)
{
opener.document.f1.p_name.value = document.forms["frm" + ctr].c_name.value;
opener.document.f1.p_name2.value = document.forms["frm" + ctr].c_name2.value;
self.close();
}
</script>
By default, mysql_fetch_array returns an array with numeric keys (i.e. 0, 1, 2, etc). If you want to retrieve records by their column name, you need to use:
while ($result = mysql_fetch_array($rs, MYSQL_ASSOC))
Look at example 3 here:
http://php.net/manual/en/function.mysql-fetch-array.php
if(isset($_POST['submit'])){
$domain=$_POST['domain'];
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$tel=$_POST['tel'];
if($domain==""){
$error="<h4>Enter Domain </h4>";
}elseif($fname == ""){
$error="<h4>Enter Firstname </h4>";
}elseif($sname == "")
{
$error="<h4 >Enter Surname</h4>";
}elseif($tel=="")
{
$error="<h4 >Enter telephono no</h4>";
}
else {
$sql11=mysql_query("INSERT INTO domain VALUES('','$domain','$fname','$sname','$tel','$mobile','$email','$company','$address','$city','$country','$pcode','$tele',
'$fax','$qus','$ans')");
echo $sql;
$db->query($sql);
}
}
<div><?php echo $error; ?></div>
<form action="" method="post" name="classic_form" id="classic_form">
<div><h4>Personal details:</h4></div><div style="margin-left: 109px;">
<div>Domain</div>
<input type="text" name="domain" id="domain" value="" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="" />
<div>Mobile:</div>
</form>
In my registration page, i used php validation. After the user submit the form if it shows validation errors it also resets all the fields. How can i resolve this problem? Without reset the fields i have to show the php validation errors. I also used in each input value. But it shows
"Notice: Undefined index: domain in D:\xampp\htdocs\deena\domainreg.php on line 82" . Please help me to resolve this problem
<input type="text" name="domain" id="domain" value="<?php echo isset($domain) ? $domain : ''; ?>" />
You have to pass all your values to php, and send back to html to feed your fields.
Its not 'resetting your fields' .. Your form is being submitted, hence the page is being reset and fields are therefore loading empty. Place the $_POST[] values in the field values upon page load:
<input type="text" name="domain" id="domain" value="<?php echo $domain ?>" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="<?php echo $fname?>" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="<?php echo $sname?>" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="<?php echo $tel?>" />
Simple. Just add the variables to the input values:
<input type="text" name="domain" id="domain" value="<?php echo $domain; ?>" />
You should also protect the outputted value, against cross site scripting:
<input type="text" name="domain" id="domain" value="<?php echo htmlspecialchars($domain); ?>" />
In the value field:
<input type="text" name="domain" id="domain"
value="<?php if(isset($_POST['domain'])){echo $_POST['domain'];} ?>">
Didn't test it. But i think it should work.
In input tag add the php value as like value="" So that it will echo if the variable is posted or it will show the empty one