When I enter a value it should be numeric only. In my code the check seems to be false because I can enter several non numeric items.
Do you have an idea ?
<body>
<form method="GET" action="">
<input type="text" name="niveau">
<input type="submit" value="valider" >
</form>
<br />
<?php
if (empty($_GET['niveau']))
{
echo 'Enter niveau please : ' . "</br>";
}
else if(isset($_GET['niveau']))
{
echo "Niveau : " .$_GET['niveau'] . "</br>";
}
else if (is_numeric($_GET['niveau']))
{
echo "Niveau is numeric" ;
}
else
{
echo "$var_name1 is not numeric. <br>" ;
}
?>
</body>
The problem is your if/else if structure. The first else if checks whether or not the niveau parameter is set. If yes you print out the niveau level and the second else if condition won't be checked.
You could for example include the second else if condition within the first one, something like this:
if (empty($_GET['niveau']))
{
echo 'Enter niveau please : ' . "</br>";
}
else if(isset($_GET['niveau']) && is_numeric($_GET['niveau']))
{
echo "Niveau : " .$_GET['niveau'] . "</br>";
}
else
{
echo "$var_name1 not set or not numeric. <br>" ;
}
Related
I have made a game flames, where example given a name is Patrick and the other name is Abcdefg, the loop will crash out one character from one name that is the same with a character in the other name. When my condition is if srtlen($name1)==strlen($name2), why is my loop not crashing out letter a in the name Patrick and abcdefg? It only crashes out c.
My problem is in if($r==$e) part.
The desired outcome from the name Patrick and abcdefg is status=10; because A and C are crashed out.
<html>
<head>
<title>Flames</title>
</head>
<body>
<center><form method="post" style="margin-top:60px;">
<h2>Flames</h2>
<input type="text" name="name1" placeholder="First name"/><br>
<input type="text" name="name2" placeholder="Second name"/><br><br>
<input type="submit" name="submit" value="submit"/>
</form>
<?php
if(isset($_POST['submit'])){
$name1=$_POST['name1'];
$name2=$_POST['name2'];
//this is if the names have space on it.
if(strstr($name1,' ')&&strstr($name2,' ')||strstr($name1,' ')||strstr($name2,' ')){
$exploded1=explode(' ',$name1);
$exploded2=explode(' ',$name2);
$joined1=implode("",$exploded1);
$joined2=implode("",$exploded2);
$e=strlen($joined2);
$r=strlen($joined1);
}
else{
$r=strlen($name1);
$e=strlen($name2);
}
$counter=0;
$same=0;
if($r>$e){
for($m=0; $m<=$e-1; $m++){
for($i=0; $i<=$r-1; $i++){
if($counter<$e){
if($joined1[$i]==$joined2[$m]){
$same++;
//$counter++;
$joined1[$i]=' ';
break;
}
}
}
}
}
elseif($e>$r){
for($m=0; $m<=$r-1; $m++){
for($i=0; $i<=$e-1; $i++){
if($counter<$r){
if($joined2[$i]==$joined1[$m]){
$same++;
//$counter++;
$joined2[$i]=' ';
break;
}
}
}
}
}
//this is where it did not check the a character which is most likely to be crashed out because it has a pair
if($r==$e){
for($m=0; $m<$r; $m++){
for($q=0; $q<$r; $q++){
if($name1[$q]==$name2[$m]){
echo $name1[$q].'<br>';
$same++;
//$counter++;
$name1[$q]=' ';
break;
}
}
}
}
$sum=$e+$r;
$mult=$same*2;
$status=$sum-$mult;
echo $joined1.'<br>';
echo $joined2.'<br>';
echo 'r'.$r; echo '<br>';
echo 'e'.$e; echo '<br>';
echo 'status'.$status; echo '<br>';
echo 'sum'.$sum;echo '<br>';
echo 'mult'.$mult; echo '<br>';
echo "$joined1 <br>";
echo "$joined2 <br>";
//this is to determine the flames status.
if($status==1||$status%6==1){
echo 'Friends';
}
if($status==2||$status%6==2){
echo 'Lovers';
}
if($status==3||$status%6==3){
echo 'Anger';
}
if($status==4||$status%6==4){
echo 'Marriage';
}
if($status==5||$status%6==5){
echo 'Enemy';
}
if($status==6||$status%6==0){
echo 'Soulmates';
}
}
?>
</center>
</body>
</html>
When you compare Patrick and Abcdefg , c will only match as capital A and small a are different ,so to solve this you can convert your names to lowercase and then compare them .i.e :
$name1=strtolower($_POST['name1']);
$name2=strtolower($_POST['name2']);
Output :
a<br>c<br>patrick<br>abcdefg<br>r7<br>e7<br>status10<br>sum14<br>mult4<br>patrick <br>abcdefg <br>Marriage
I can't access array from other file and still don't figure it out, whether my data already stored into array or not. Should i put $_SESSION into the function?
Lat3_3a.php
<form id="form1" name="form1" method="post" action="Lat3_3b.php">
Insert number: <input type="number" name="num" id="num" />
<input type="submit" name="button" id="button" value="OK" />
</form>
Lat3_3b.php
<?php
session_start();
$_SESSION["num"] = $_POST["num"];
if (empty($_SESSION["num"]))
echo "Please, insert number";
else {
$val=$_POST['num'];
echo " Factorial " .$val. " ! = " .factorial($val)."<br/>";
echo "<a href='Lat3_3c.php'>Link</a>";
}
function factorial($val){
if($val<=1){
$result=1;
return $result;
}elseif($val>1){
for($i=1; $i<=$val; $i++){
$result=$val * factorial($val-1);
}
return $result;
}
$data=array($val,$result,"12345", "Travis");
$_SESSION["var"]=$_POST["data"];
}
?>
Lat3_3c.php
<?php
session_start();
if(empty($_SESSION["var"]))
echo "Variable not found";
else
echo "Data : ". $_SESSION["var"];
?>
Ok looking a bit more in to your code, you need to learn more about the session, post and functions. For that a look at the PHP manual site.
Site http://php.net
For your code to work you need to use something like this:
<?php
session_start();
$_SESSION["num"] = $_POST["num"];
$_SESSION["var"]=array();
if (empty($_SESSION["num"])){
echo "Please, insert number";
}
else {
$val=$_POST['num'];
$function_result=factorial($val);
$data=array($val,$function_result,"12345","Travis");
$_SESSION["var"]=$data;
echo " Factorial " .$val. " ! = " .$function_result."<br/>";
echo "<a href='Lat3_3c.php'>Link</a>";
}
function factorial($val){
if($val<=1){
$result=1;
return $result;
}elseif($val>1){
for($i=1; $i<=$val; $i++){
$result=$val * factorial($val-1);
}
return $result;
}
}
?>
I'm not looking into your math.
I have made a voice dictionary. The user inputs a word and the corresponding meaning is fetched from my sql database , it is displayed on screen and then converted to speech.Its working all fine.
I just want to add one condition that : If user enters a word that is not present in my database then it outputs the message " sorry word not found", and nothing should be played in the audio.
Currently if i enter a word which is not present , i am getting the audio of 'undefined index..something.. '
plz tell me where to add the if condition and what to add
Here is my code
<html>
<head>
<title>Word meanings</title>
<?php
mysql_connect("localhost", "root", "abcd");
mysql_select_db("dictionary");
if(isset($_POST['Submit1']))
{
$req=$_REQUEST['word'];
$strSQL = "SELECT * FROM dict WHERE word='$req'";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs))
{
$x=$row["word"];
$y=$row["meaning"];
$z=$row["synonym"];
echo "<b>Word</b>: " . $x ."<br/>" ;
echo "<b>Meaning</b>: " . $y ."<br/>" ;
echo "<b>Synonym</b>: " . $z ."<br/>" ;
}
}
mysql_close();
?>
</head>
<body>
<form name="form1" action="lastry.php" method="POST">
<input type="text" name="word" value="<?php echo isset($_POST['word'])?$_POST['word']:''?>"x-webkit-speech/>
<Input Type ="Submit" Name ="Submit1" Value ="submit">
</form>
<?php
if($_POST)
{
?>
<p>Listen word
<audio controls="controls">
<source src="http://speechutil.com/convert/ogg?text='<?php echo urlencode($x);?>'" &type="audio/mp3" />
</audio>
</p>
<br><br>
<p>Listen meaning
<audio controls="controls">
<source src="http://speechutil.com/convert/ogg?text='<?php echo urlencode($y);?>" &type="audio/mp3" />
</audio>
</p>
<br><br>
<p>Listen synonym
<audio controls="controls">
<source src="http://speechutil.com/convert/ogg?text='<?php echo urlencode($z);?>'" &type="audio/mp3" />
</audio>
</p>
<?php
}
?>
</body>
</html>
Immediately after $rs = mysql_query($strSQL);, add the line $num_rows = mysql_num_rows($rs);, then you can have:
if($num_rows > 0) {
/*Run while loop which you already have*/
}
else {
echo 'Sorry word not found';
}
maybe check if it has value in it
if( isset($_POST) && !empty($x) )
Add counter to here:
$foundwords = 0;
while($row = mysql_fetch_array($rs))
{
$x=$row["word"];
$y=$row["meaning"];
$z=$row["synonym"];
echo "<b>Word</b>: " . $x ."<br/>" ;
echo "<b>Meaning</b>: " . $y ."<br/>" ;
echo "<b>Synonym</b>: " . $z ."<br/>" ;
$foundwords++; // increase counter after each found word
}
and then later after mysql_close() check that variable (somewhere inside the body)
if($foundwords==0) {
echo 'sorry word not found';
... finish html etc ...
die();
}
you should do something like this :
<?php
if (isset($_POST['Submit1']) {
mysql_connect("localhost", "root", "abcd");
mysql_select_db("dictionary");
$req=$_REQUEST['word'];
$rs = mysql_query("SELECT * FROM dict WHERE word='$req'");
while($row = mysql_fetch_array($rs)){
$x=$row["word"];
$y=$row["meaning"];
$z=$row["synonym"];
echo "<b>Word</b>: " . $x ."<br/>" ;
echo "<b>Meaning</b>: " . $y ."<br/>" ;
echo "<b>Synonym</b>: " . $z ."<br/>" ;
}
if (!isset($x)) { $All_Find = $req; } else { $All_Find = true; }
}
?>
<?php if ($All_Find == true) { ?>
<audio controls="controls">
<source src="http://speechutil.com/convert/ogg?text='<?php echo urlencode($y);?>" &type="audio/mp3" />
</audio>
<?php } else { echo "Sorry, the world $All_Find cannot be found :(";}?>
The undefined index message is mainly the result of your php.ini settings. If you turn of Notice are Warning error levels there. The value of a no existing associative array member in php is always NULL ( = false = '' ).
If you want to be exact however, you could check like this
// the condition evaluates to false if $row has no 'meaning' member
if($row['meaning'])
$z = $row['meaning'];
else
$z = "sorry word not found";
It was working until I tried adding this statement
<?php echo "<input type=\"hidden\" name=\"hidden1\" value=\"$id\">" ?>
I was able to get the indexnum from my form before but when I added that line nothing seems to load in the fields.
Here is the full form:
<form name="approveform" method="POST" action="">
<?php echo "<input type=\"hidden\" name=\"hidden1\" value=\"$id\">" ?>
Index Number*: <input type="text" name="IndexNum">
<input type="submit" value="Approve" action="">
</form>
Its getting late and I probably need to just go to sleep but IM SO CLOSE!!! Here is the full code that processes the POST.
if($user_data['permissions'] >= 1)
{
// If users permission is 1 or 2 they get a field for inputting the index # and a button to change the approve field from 0 to 1 may need to make a new field to record who approved it....
//Determine if the order is already approved. If not approved show index field and allow user to approve it with index number
if($data2[0]['Approved'] == 1)
{
echo " <font color=\"green\"> Approved";
}
if($data2[0]['Approved'] == 0)
{
echo " Not Approved. Supply an index number and click approve to authorize this order to be completed.";
if (empty ($_POST) === false)
{
$required_fields = array('IndexNum');
foreach ($_POST as $key=>$value)
{
if (empty($value) && in_array($key, $required_fields) === true)
{
$errors[] = 'Fields marked with an asterisk are required';
break 1;
}
}
if (isset($_POST['success']) === true && empty($_POST['success']) === true)
{
echo 'Index has been updated and Order is now set to Approved';
}
else
{
if (empty($errors) === true)
{
$indexnum=$_POST['IndexNum'];
$approvedby=$user_data['lname'];
$vendorid1= $_POST['hidden1'];
echo $indexnum;
echo $approvedby;
echo $vendorid1;
//update_approved($indexnum, $approvedby, $vendorid1);
//header('Location: index.php');
//exit();
}
else if(empty($errors) === false)
{
echo output_errors($errors);
}
}
}
?>
<form name="approveform" method="POST" action="">
<?php echo "<input type=\"hidden\" name=\"hidden1\" value=\"$id\">" ?>
Index Number*: <input type="text" name="IndexNum">
<input type="submit" value="Approve" action="">
</form>
<?php }
}
Thank you all for looking into this. I get that $id value from a previous POST. I use it elsewhere in the code without issue.
Thank you all so much!
Try like
<input type="hidden" name="hidden1" value="<?php echo $id;?>">
Also try like
<?php echo '<input type="hidden" name="hidden1" value=".$id.">' ?>
I've been at this the whole day. First I tried to validate my form in php but finally gave up. Now I'm trying to validate the form with JavaScript. It works but since I don't know JavaScript at all and have limited knowledge of PHP I really need help please. It is a test and I want to validate that each question has been answered. The field name in the form = php variable/array, can't get this to work in the javascirpt
var x=document.forms["myForm"]["question_{$q_nr}"].value;
. If I just use a text field name it works fine.
The form
<html>
<head>
<?php
session_start();
?>
<script type="text/javascript" src="counter.js"></script>
<script language="JavaScript">
<!--
function validateForm()
{
var x=document.forms["myForm"]["question_{$q_nr}"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}
//-->
</script>
</head>
<body>
<?php
if ($_SESSION['auth']) {
$tid = $_GET['tid'];
echo "<span id='counter'></span>";
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY q_nr";
$result1=mysql_query($sql1);
echo "<form name='myForm' action='http://localhost/index.php?option=com_content&view=article&id=51' onsubmit='return validateForm()' method='post'>";
while($row1 = mysql_fetch_array($result1))
{
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];
echo "<P><strong>$q_nr $question</strong><BR>";
echo "<img src='images/tests/$pic'>";
echo "<BR>";
echo "<BR>";
echo "</p>";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question_{$q_nr}' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='radio' name='question_{$q_nr}' value='B'>$option2<BR>";
} else {
echo ''; }
} else { // else if not <> mr
if($option1!="") {
echo "<input type='checkbox' name='question_{$q_nr}[]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='checkbox' name='question_{$q_nr}[]' value='B'>$option2<BR>";
} else {
echo ''; }
} //if q_type <> mr
} //while row1
echo "First name: <input type='text' name='fname'>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
} //else if now > testend
} //if ses auth
?>
why do you think so complicated ?
just use unique ID's for each label, input field, etc ... and check for the innerHTML or value stored; and also you can change the values whenever you want;
btw, your code is messy;
write your code like this:
zone 1: all the js goes here
zone 2: all the html goes here
zone 1:
<script language="text/JavaScript">
//alljs code here
function validate()
{
if( document.getElementById('unique_2').value == null ) document.getElementById('unique_3').innerHTML = 'error1';
if(document.getElementById('unique_4').value == null ) document.getElementById('unique_6').innerHTML = 'error 2';
}
</script>
zone 2:
<form onsubmit="validate()">
all html label and fields go here, like:
<label id="unique_1">label text</label>
<input type="text" id="unique_2" value="" />
<label id="unique_3"></label> this is for the error message
<label id="unique_4">label text</label>
<input type="text" id="unique_5" value="" />
<label id="unique_6"></label> this is for the error message
</form>
This is simple JQuery validation.
I think you must have both validation if you want to be secured...
p.s. Just add 'required' in class of input if you want to be required...
Well for one thing -- validating on the server side is the way to go.
If you just validate on the client side, it can be futzed with (a lot)