php word check if they are the same - php

I need that they check my word witch i written in text field. If 2 words the same i should get message - "They are the same".
<?php
function array_random($arr, $num = 1) {
shuffle($arr);
$r = array();
for ($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return $num == 1 ? $r[0] : $r;
}
$a = array("snow", "ball", "side");
//print_r(array_random($a));
//print_r(array_random($a, 3));
?>
<form name='form' method='post' align = "center">
<?php
$zod = (array_random($a)); echo $zod; ?> <input type="text" name="name" id="name" ><?php
if((isset($_POST['name'])) && !empty($_POST['name']))
{
$name = $_POST['name'];
echo ' '.$name;
}
?><br/><br>
<input name="select" type="submit" onclick="select()" value="select" />
</form>

A simple comparison uses the '==' operator. These can be used to compare something. You want to compare a posted value with the one from your array. which is random:
<?php
function array_random($arr, $num = 1) {
shuffle($arr);
$r = array();
for ($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return $num == 1 ? $r[0] : $r;
}
if ( isset ( $_POST['submit'] ) && isset ( $_POST['name'] ) ) {
if ( $_POST['compare'] == $_POST['name'] )
echo "They are the same, hurray!<br />";
else
echo "These don't match!";
}
$a = array ( "snow", "ball", "side" );
$rand = array_random ( $a );
?>
<form name='form' method='post' align = "center">
Please type the word: <?php echo $rand; ?>
<input type="text" name="name" id="name" >
<input type="hidden" name="compare" id="compare" value="<?php echo $rand ?>" >
<input name="submit" type="submit" onclick="select()" value="select" />
</form>
As you wanted, a random value is picked and compared to what the user posted.

Related

How to do 3*3 matrix multiplication in php?

How can i do 3*3 or 3*1 matrix multiplication in php with following code. where should i have to change the logic ?????
this code completely work for only 2*2 matrix multiplication
Here is my code:
$a = Array( Array(1,2),Array(4,5));
$b = Array( Array(7,5), Array(3,2));
$sumArray = array();
$c = array();
for($i=0;$i<2;$i++)
{
for($j=0;$j<2;$j++)
{
$c[$i][$j]=0;
for($k=0;$k<2;$k++)
{
$c[$i][$j]=$c[$i][$j]+($a[$i][$k]*$b[$k][$j]);
}
}
}
echo "<pre/>";
print_r($c);
?>
Matrix Multiplication
Rules :
First matrix's column and Second matrix's row must be same
Result matrix's size will be First matrix's Row and Second matrix's Column
$a = Array( Array(1,2),Array(4,5));
$b = Array( Array(7,5), Array(3,2));
$r=count($a);
$c=count($b[0]);
$p=count($b);
if(count($a[0]) != $p){
echo "Incompatible matrices";
exit(0);
}
$result=array();
for ($i=0; $i < $r; $i++){
for($j=0; $j < $c; $j++){
$result[$i][$j] = 0;
for($k=0; $k < $p; $k++){
$result[$i][$j] += $a[$i][$k] * $b[$k][$j];
}
}
}
print_r($result);
Matrix computation is achieved by multiplying any column or row elements by their own cofactors.
Codes according to your request;
<?php
if ($_POST['hsp']) {
$de3t11=$_POST['m11']; $de3t12=$_POST['m12']; $de3t13=$_POST['m13'];
$de3t21=$_POST['m21']; $de3t22=$_POST['m22']; $de3t23=$_POST['m14'];
$de3t31=$_POST['m31']; $de3t32=$_POST['m23']; $de3t33=$_POST['m15'];
//show on screen
echo $de3t11 ." ". $de3t12. " ". $de3t13;
echo "<br>";
echo $de3t21 ." ". $de3t22. " ". $de3t23;
echo "<br>";
echo $de3t31 ." ". $de3t32. " ". $de3t33;
echo "<br>";
//calculate matrix
$m3m11=(($de3t22*$de3t33)-($de3t23*$de3t32))*(1);
$m3m12=(($de3t21*$de3t33)-($de3t23*$de3t31))*(-1);
$m3m13=(($de3t21*$de3t32)-($de3t22*$de3t31))*(1);
//Determinant
$det3t3=(($de3t11*$m3m11)+($de3t12*$m3m12)+($de3t13*$m3m13));
echo $det3t3;
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="POST">
<input type="number" name="m11">
<input type="number" name="m12">
<input type="number" name="m13">
<br><br>
<input type="number" name="m21">
<input type="number" name="m22">
<input type="number" name="m23">
<br><br>
<input type="number" name="m31">
<input type="number" name="m32">
<input type="number" name="m33">
<input type="submit" name="hsp">
</form>
</body>
</html>

Display result in same field that had user input [PHP]

Hello I have a simple page that allows a user to enter a binary value which is then converted into a decimal value. However, I am using two fields, one for entering the binary value, and the other to display the result in decimal.
I want to only use on field, so when the user enters the binary value, the result in decimal will appear in the same box that they entered input. Here is my current code:
<?php
if (isset($_POST['submit']))
{
$str = $_POST['binToDec'];
$pos = 0;
$sum = 0;
$tempSum = 0;
$strLength = strlen($_POST['binToDec']) - 1;
$powerOfTwo = 1;
if ( $str{strLength} == 1 )
$sum += 1;
for ( $i = $strLength - 1; $i >=0; $i-- )
{
$tempSum = ($powerOfTwo *= 2);
if ( $str{$i} == 1 )
$sum += $tempSum;
}
}
?>
<html><body>
<form method="post" action="#">Binary value: <input name="binToDec"><br />
Result: <input value="<?php if (isset($sum)) echo $sum ?>"><br />
<input type="submit" name="submit" value="Convert">
</form>
<body></html>
This this little modification will solve your issue:
<form method="post" action="#">
Binary value: <input name="binToDec" value="<?php if (isset($sum)) echo $sum ?>"><br />
<input type="submit" name="submit" value="Convert">
</form>
This code is setting value on the input you've used for the POST action of the form.
Here's the code:
<?php
if (isset($_POST['submit']))
{
$str = $_POST['binToDec'];
$pos = 0;
$sum = 0;
$tempSum = 0;
$strLength = strlen($_POST['binToDec']) - 1;
$powerOfTwo = 1;
if ( $str{strLength} == 1 )
$sum += 1;
for ( $i = $strLength - 1; $i >=0; $i-- )
{
$tempSum = ($powerOfTwo *= 2);
if ( $str{$i} == 1 )
$sum += $tempSum;
}
}
?>
<html><body>
<form method="post" action="#">
Binary value: <input name="binToDec" value="<?php if (isset($sum)) echo $sum ?>" />
<input type="submit" name="submit" value="Convert">
</form>
<body></html>
Try to use the onkeyup or onkeydown or onchange to show the value in the same box, using
javascript,
example,
function Conversion(num){
return num.split('').reverse().reduce(function(x, y, i){
return (y === '1') ? x + Math.pow(2, i) : x;
}, 0);
}
Enter: <input id="bin" name="" type="text" onkeyup="Conversion(this)" size=20>
Store return value to the same text box using the javascript like by using document.getElementbyId

Processing radio buttons in loop in PHP

I have a loop that iterates 3 times. Inside the loop I have a HTML form that has radio buttons. I'm processing the input using PHP. When I echo the form data, its not showing the correct values. Is it a wrong way of processing the data ? Any help is appreciated.
test.php
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<form action = 'test.php' method = 'post'>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
</form>
<?php
}
?>
<input type = 'submit' value = 'Go'>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
Move your form outside of your for loop. You are currently creating three forms with one submit button (which isn't attached to any of them).
try this way
<form action = 'test.php' method = 'post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
use following code: Or you can use radiogroup array it is easier than this.
<form action = 'test.php' method = 'post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
#Cagy79 added additional submit buttons.. I have revised the code,
<form action ="" method='post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>
This works. :)
You have 3 forms without a submit button (it needs to be in between the tags). You need to create one form with one submit button like this so all your data is posted to $_POST.
<form method='post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
<input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
<input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>
<input type = 'submit' value = 'Go'>
</form>
<?php
for ($i = 1; $i <= 3; $i++) {
echo $_POST['num' . $i];
}
?>

Form not outputting number [duplicate]

This question already has an answer here:
Output of numbers are incorrect sometimes.
(1 answer)
Closed 9 years ago.
Given an input of an expression consisting of a string of letters and operators (plus sign, minus sign, and letters. IE: ‘b-d+e-f’) and a file with a set of variable/value pairs separated by commas (i.e: a=1,b=7,c=3,d=14) write a program that would output the result of the inputted expression.
For example, if the expression input was ("a + b+c -d") and the file input was ( a=1,b=7,c=3,d=14) the output would be -3.
Thus far I have got this, it calculates the numbers but not sure how to calculate it by the letters abcd,
<html>
<head>
</head>
<body>
<form action = "" method = "GET">
Number 1: <input type = "text" name = "input[]" size=3>
<br/>
Number 2: <input type = "text" name = "input[]" size=3> <br>
Number 3: <input type = "text" name = "input[]" size=3> <br>
Number 4: <input type = "text" name = "input[]" size=3> <br>
<input type="hidden" name="calc" value ="yes">
<input type = "submit" name = "Calculate"/>
</form>
</body>
</html>
<?php
print_r($_GET);
$myInputs = $_GET['input'];
print_r($myInputs);
$myArray = array();
$myArray['a'] = 5;
$myArray['b'] = 10 ;
$myArray['c'] = 15 ;
$myArray['d'] = 20 ;
echo $myArray[$_GET['a']] + $myArray[$_GET['b']] ;
/*
if ($_GET['calc'] == "yes")
{
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
$d = $_GET['d'];
$total = $a+$b+$c+$d;
print "Total is: $total <p>";
}
*/
?>
<?php
$expression = '';
$input = array( 'a' => 5,
'b' => 10,
'c' => 15,
'd' => 20);
$total = 0;
$sum = '';
// Override default expression and input values
if(isset($_POST['Calculate']))
{
$input = $_POST['input']; //input being passed via POST method
$expression = $_POST['expression']; //expression being passed via POST method
}
// make sure the users expression is safe to use
if(preg_match('#^[a-z+-\s]+$#i', $expression))
{
$sum = $expression; //evaluates sum as expression
$input_letters = array(); //puts letters into a array
$input_operators = array(); //puts operators into a array
for ($i = 0; $i < strlen($expression) ; $i++) { //gets string length
if (($i % 2) == 0) {
$input_letters[] = $expression[$i]; //getting the character for the position i
} else {
$input_operators[] = $expression[$i];
}
}
//
for ($i = 0 ; $i < sizeof($input_letters); $i++) { //size of= numbers (4)
$value = $input[$input_letters[$i]]; //takes the value from the letters
if ($i == 0) {
$sum += $value; //adds the sum if = 0
} else {
if ($input_operators[$i-1] == '+') { //checks to see if the operator is +
$sum += $value;
} else if ($input_operators[$i-1] == '-') { //checks to see if the operator is -
$sum -= $value;
}
}
print_r($value);
}
} else {
echo "Error: expression not correct form"; //error message
}
?>
<form action="" method="post"> <!--Form begins-->
Number A: <input type="text" name="input[a]" value="<?php echo $input['a']; ?>" size=3> <br/>
Number B: <input type="text" name="input[b]" value="<?php echo $input['b']; ?>" size=3> <br>
Number C: <input type="text" name="input[c]" value="<?php echo $input['c']; ?>" size=3> <br>
Number D: <input type="text" name="input[d]" value="<?php echo $input['d']; ?>" size=3> <br>
<br>
Expression: <input type = "text" name = "expression" value="<?php echo $expression ?>"> =
<?php echo $sum; ?><br>
Sum: <?php echo $sum; ?><br>
<input type="submit" name="Calculate" />
</form>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$( "form" ).submit(function( event ) {
var val_is_good=($('input[name="expression"]').val().length==4)?true:false;
if(!val_is_good){alert('Enter four letters or die!');}
});
</script>
Now I added a script to tell users if they enter more then 4 letters a alert message says please only enter four letters, would someone know why my script is outputing the alert message even when enter only four letters?
<html>
<head>
</head>
<body>
<form action = "" method = "GET">
Number 1: <input type = "text" name = "input[]" size=3>
<br/>
Number 2: <input type = "text" name = "input[]" size=3> <br>
Number 3: <input type = "text" name = "input[]" size=3> <br>
Number 4: <input type = "text" name = "input[]" size=3> <br>
<input type="hidden" name="calc" value ="yes">
<input type = "submit" name = "Calculate"/>
</form>
</body>
</html>
<?php
print_r($_GET);
$myInputs = $_GET['input'];
//you are assigning $_GET values to an array myInputs so no need here
print_r($myInputs);
echo $myInputs[0]+ $myInputs[1];
$myArray = array();
$myArray['a'] = 5;
$myArray['b'] = 10 ;
$myArray['c'] = 15 ;
$myArray['d'] = 20 ;
// In case of myarray it has keys a,b,c,d only... so need of using $_GET here also.
echo $myArray['a']+$myArray['b'];
// but if you want to it directly from$_GET values.
echo $_GET['input'][0]+ $_GET['input'][1];
?>
I found a solution at this site. Here is the code from that post:
<?php
$expression = '';
$input = array( 'a' => 5,
'b' => 10,
'c' => 15,
'd' => 20);
$total = 0;
$sum = '';
// Override default expression and input values
if(isset($_POST['Calculate']))
{
$input = $_POST['input'];
$expression = $_POST['expression'];
}
// make sure the users expression is safe to use
if(preg_match('#^[a-z+-\s]+$#i', $expression))
{
$sum = $expression;
$input_letters = array();
$input_operators = array();
for ($i = 0; $i < strlen($expression) ; $i++) {
if (($i % 2) == 0) {
$input_letters[] = $expression[$i];
} else {
$input_operators[] = $expression[$i];
}
}
//
for ($i = 0 ; $i < sizeof($input_letters); $i++) {
$value = $input[$input_letters[$i]];
if ($i == 0) {
$sum += $value;
} else {
if ($input_operators[$i-1] == '+') {
$sum += $value;
} else if ($input_operators[$i-1] == '-') {
$sum -= $value;
}
}
}
} else {
echo "Error: expression not correct form";
}
?>
<form action="" method="post">
Number A: <input type="text" name="input[a]" value="<?php echo $input['a']; ?>" size=3> <br/>
Number B: <input type="text" name="input[b]" value="<?php echo $input['b']; ?>" size=3> <br>
Number C: <input type="text" name="input[c]" value="<?php echo $input['c']; ?>" size=3> <br>
Number D: <input type="text" name="input[d]" value="<?php echo $input['d']; ?>" size=3> <br>
<br>
Expression: <input type = "text" name = "expression" value="<?php echo $expression ?>"> =
<?php echo $sum; ?><br>
Sum: <?php echo $sum; ?><br>
<input type="submit" name="Calculate" />
</form>

HTML form multiple fields

I have html form that looks like:
<form method="post" action="?a=up">
...some mysql query...
while ($i = mysql_fetch_array($result)) {
<input name="name[]" type="text" value="<?=$i['name'];?>" />
<input name="years[]" type="text" value="<?=abs($age);?>"/>
<input name="to[]" type="checkbox" value="<?=$i['id'];?>" />
}
<input name="" type="submit" value="go" />
</form>
The problem I have is that I can not get the values of the form fields such as "name" and "years".
I can only get a list of the ids (value of "to" checkbox).
The php code looks like:
$cnt = 0;
for($p = 0; $p <= (sizeof($to)-1); $p++)
{
echo $to[$p].$name[$p].$years[$p]"<br>";
$cnt++;
}
$tm = array($cnt);
What I'm doing wrong?
You're expecting a checkbox to be a successful control even if it isn't checked (and the specification says that it must not be so).
You should probably do something along the lines of:
<input name="name[<?php echo htmlspecialchars($i['id']); ?>]"
value="<?php echo htmlspecialchars($i['name']); ?>" />
<input name="years[<?php echo htmlspecialchars($i['id']); ?>]"
value="<?php echo abs($age);?>"/>
Update
Here is how you can get checkboxes that are checked using isset:
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
$cnt = 0;
for($p = 0; $p <= (sizeof($_POST['to'])-1); $p++)
{
if (isset($_POST['to'][$p]))
{
echo $_POST['to'][$p] . $_POST['name'][$p] . $_POST['years'][$p] . "<br>";
$cnt++;
}
}
$tm = array($cnt);
}
You are not getting the fields from POST array, here is how your code should be:
$cnt = 0;
for($p = 0; $p <= (sizeof($_POST['to'])-1); $p++)
{
echo $_POST['to'][$p] . $_POST['name'][$p] . $_POST['years'][$p] . "<br>";
$cnt++;
}
$tm = array($cnt);
Make sure that above code executes when form is submitted by putting it in this condition:
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
$cnt = 0;
for($p = 0; $p <= (sizeof($_POST['to'])-1); $p++)
{
echo $_POST['to'][$p] . $_POST['name'][$p] . $_POST['years'][$p] . "<br>";
$cnt++;
}
$tm = array($cnt);
}
And finally a little suggestion that you should avoid using short php tags <?=?> for they have posed security issues and can easily be embedded into images or xml. (Make sure that they are also turned on from php.ini if you want to use them)
Why dont you just try
$cnt = 0;
foreach ( $_POST['to'] as $k => $to ){
echo $_POST['to'][$k] . $_POST['name'][$k] . $_POST['years'][$k] . "<br />";
$cnt ++;
}
$tm = array ($cnt);

Categories