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;
?>
Related
Im making a form that checks the Name input and echos results when the submit button is pressed using a IF statement. But i cant get it to work. What am i doing wrong?
<form id="AUTO" method="post" action="" novalidate>
<input type="hidden" name="token" value="<?php echo $token; ?>"/>
<input type="hidden" name="miles" value=""/>
<div id="contact_name">FULL NAME: *<br>
<input id="element_2_1" name="name" class="element text" size="15"
maxlength="15" value="" type="text" placeholder="FULL NAME">
</div>
<input type="submit" name="submit" class="submit action-button" value="SUBMIT" />
</form>
$Name = $_POST['Name'];
if (isset($_POST['submit'])) {
//checks name field for a number if one exist echo has a number
if (preg_match('|[0-9]', $Name)) {
echo 'has a number';
} else {
echo 'Does not contain a number';
}
}
The form input tag is named name however you are trying to get value POST index of Name.
Correct would be
$Name = $_POST['name'] ;
Keep the case of names.
Forms are key are sensitive if you set the form name as lowercase
<input id="element_2_1" name="name" class="element text" size="15"
maxlength="15" value="" type="text" placeholder="FULL NAME">
and in server side you should catch it as lowercase like this below.
$name = $_POST['name'];
That's how you can call the variable from the form in your server side.
Hope this helps you to solve your problem.
Thanks
<form id="AUTO" method="post" action="" novalidate>
<input type="hidden" name="token" value="<?php echo $token; ?>"/>
<input type="hidden" name="miles" value=""/>
<div id="contact_name">FULL NAME: *<br>
<input id="Name" name="Name" class="element text" size="15" maxlength="15" type="text" placeholder="Full Name" value=""></div>
<input type="submit" name="submit" class="submit action-button" value="SUBMIT" />
</form>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$Name = $_POST['Name'] ;
if (preg_match('#[0-9]#',$Name)){
echo 'has number';
}else{
echo 'no number';
}
?>
I wish every time I reload or refresh the page. The disabled textbox still disabled. Because my purpose is to modify the textbox once time and then it will be disabled forever. Is this possible?
Below is my code
<tr>
<td><?php echo $lang_txt['leader_id'][$lang]; ?></td>
<td>:</td>
<td><?php if($_POST['txtLeaderID'] == ""){?><input type="text" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="<?php echo $merchant['LeaderID']; ?>" maxlength="20" /><?php }
else
{
?>
<input type="text" disabled="disabled" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="<?php echo $merchant['LeaderID']; ?>" maxlength="20" /><?php
}
?>
</td>
</tr>
Your variant will be worked with
if(!$_POST['txtLeaderID']){echo('<input type="text" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="'.$merchant['LeaderID'].'" maxlength="20" />')} else {echo('<input type="text" disabled="disabled" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="'.$merchant['LeaderID'].'" maxlength="20" />')}
For example:
<?php
if (!$_POST['username']){
echo('
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
Name: <input type="text" name="username" /><br />
Email : <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Send POST!" />
</form>
');}
else{
echo('
<form>
Name: <input type="text" name="username" disabled="disabled"/><br />
Email : <input type="text" name="email" disabled="disabled"/><br />
</form>
');}
?>
But it didn't work, if we open it in a new tab. You must save this propery anywhere (session, cookie, sql)and check the stored values. it will be work in a new tab.
For example with session:
<?php
session_start();
if (!isset($_SESSION['data'])) {
$_SESSION['data'] = true;
echo('
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
Name: <input type="text" name="username" /><br />
Email : <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Send POST!" />
</form>
');}
else{
echo('
<form>
Name: <input type="text" name="username" disabled="disabled"/><br />
Email : <input type="text" name="email" disabled="disabled"/><br />
</form>
');}
?>
<form action="chngschdl.php" method="post">
<fieldset>
<label class="home">Flight Name</label> <select name="select_catalog_query" ><?php echo $options1; ?></select>
<br/><br/>
<label class="home">Starting Route</label> <input class="text" type="text" name="stroute" onKeyUp="numericFilter(this);" /> <label class="home">Deperture Time</label> <input class="text" type="text" name="stdrt" /><br/>
<label class="home">Ending Route</label> <input class="text" type="text" name="enroute" onKeyUp="numericFilter(this);" /> <label class="home">Arrival Time</label> <input class="text" type="text" name="enart" /><br/>
<label class="home">Break Route Number</label> <input class="text" type="number" name="bpn" maxlength='1' onkeyup="Bpoint(this.value)" /><br/>
<label id='bp' > <?php //$i=$_SESSION['point'];$_SESSION['bp']=$broute[$i];?></label>
<button class="btn">Go</button>
</fieldset>
</form>
ajax getting data from:
<?php
session_start();
if( $_SESSION['type']!='admin')
{
header("Location: index.php");
return;
}
$point=$_GET['q'];
$_SESSION['point']=$point;
$bpoption="";
for($i=0;$i<$point;$i++)
{
echo "<label>Break Route[$i]<label> <input class=text type=text name=broute[$i] /> ";
echo "<label>Arrival Time[$i]<label> <input class=text type=text name=bart[$i] /> ";
echo "<label>Departure Time[$i]<label> <input class=text type=text name=bdrt[$i] /> ";
echo "<br/><br/>";
}
?>
i have to collect every data of Break Route, Arrival time and Departure time... how i suppose to get this data and use it in another page?? is there is other way around by not using ajax... bt it must be remembered that Break Route is not predefined...
since your form fields are named like "name[$i]" it doesn't matter how many ajax-populated fields there are.
All you have to do on the page processing the form submit is:
foreach ($_POST["name"] as $name) {
// do things
}
I want to make a text field and button that will allow the user to fetch his details on the text fields instead of writting his details every time he wants to make a new reservation.
like in this picture:
http://oi41.tinypic.com/23ie70j.jpg
I tried to make this but with my code but gives me double forms one with the details and one without.
<form method="post" action="reserv page.php">
enter the email: <input type = "text" name = "email"/>
<input type = "submit" name = "submit" value="submit" />
</form>
<?php
mysql_connect("localhost","userName","password");
mysql_select_db("database_Name");
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$q = "SELECT * FROM tabe WHERE the_email = '$email'";
$run = mysql_query($q );
while($row = mysql_fetch_array($run))
{
?>
</br></br>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" value="<?php echo $row[1]; ?>" />
Last Name: <input name="lNamet" type="text" value="<?php echo $row[2]; ?>" />
User Name: <input name="uName" type="text" value="<?php echo $row[3]; ?>"/>
Email: <input name="email" type="text" value="<?php echo $row[4]; ?>" />
password: <input name="pass" type="password" value="<?php echo $row[5]; ?>"/>
contact: <input name="number" type="text" value="<?php echo $row[6]; ?>" />
<input name="confirm" type="submit" value="Confirm" />
</form>
</br></br>
<?php
}}
?>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" />
Last Name: <input name="lNamet" type="text" />
User Name: <input name="uName" type="text" />
Email: <input name="email" type="text" />
password: <input name="pass" type="password" />
contact: <input name="number" type="text" />
<input name="confirm" type="submit" value="Confirm" />
</form>
<form method="post" action="reserv page.php">
enter the email: <input type = "text" name = "email"/>
<input type = "submit" name = "submit" value="submit" />
</form>
<?php
mysql_connect("localhost","userName","password");
mysql_select_db("database_Name");
if(isset($_POST['submit']))
{
$email = $_POST['email'];
//limit the query to one entry :)
$q = "SELECT * FROM tabe WHERE the_email = '$email' LIMIT 1";
$run = mysql_query($q );
//check if email is registered
if(mysql_num_rows($run)>0)
{
//display filled up form
while($row = mysql_fetch_array($run))
{
?>
</br></br>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" value="<?php echo $row[1]; ?>" />
Last Name: <input name="lNamet" type="text" value="<?php echo $row[2]; ?>" />
User Name: <input name="uName" type="text" value="<?php echo $row[3]; ?>"/>
Email: <input name="email" type="text" value="<?php echo $row[4]; ?>" />
password: <input name="pass" type="password" value="<?php echo $row[5]; ?>"/>
contact: <input name="number" type="text" value="<?php echo $row[6]; ?>" />
<input name="confirm" type="submit" value="Confirm" />
</form>
</br></br>
<?php
}
}
//display blank form
else{
?>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" />
Last Name: <input name="lNamet" type="text" />
User Name: <input name="uName" type="text" />
Email: <input name="email" type="text" />
password: <input name="pass" type="password" />
contact: <input name="number" type="text" />
<input name="confirm" type="submit" value="Confirm" />
</form>
<?php
}
}
?>
You're getting 2 forms because you're echoing one form if $_POST['submit'] is set and then another one regardless of anything. Print the second form only if $_POST['submit'] is not set. Since your code is so poorly written I will just give you an example:
if(isset($_POST['submit'])){
PRINT FETCHED FORM
}else{
PRINT EMPTY FORM
}
This, however, is not the "right" way to go. What people actually do is have variables null'd at start and then fill them up with data if there's a request and have a single form written in the file with input values as those variables.
I've got a system where I allow a user to select multiple checkboxes from n amount of checkboxes, but also want two more inputs associated with each checkbox. This is for a message, a date and a time. When I post the data to be processed by a PHP script, I'd like to be able to access each of the sets of checkbox and two other inputs so I can see what date and time a user has filled in for each of the messages they've selected. I'm having trouble coming up with a method to associate the two other inputs with each checkbox.
Any ideas how to do this?
You can use arrays in your html inputs like so...
<input type="text" name="messages[1][message]" value="herp" />
<input type="text" name="messages[1][date]" value="24th April" />
<input type="text" name="messages[1][time]" value="13:00" />
<input type="text" name="messages[2][message]" value="derp" />
<input type="text" name="messages[2][date]" value="26th April" />
<input type="text" name="messages[2][time]" value="18:00" />
<?php
$messages = $_REQUEST['messages'];
foreach ($messages as $messageId => $value){
echo $value['message'];
echo $value['date'];
echo $value['time'];
}
Your HTML:
<input type="checkbox" name="car" />
<input type="text" name="msg_car" value="Car message" />
<input type="text" name="date_car" value="Car date" />
<input type="checkbox" name="bike" />
<input type="text" name="msg_bike" value="Bike message" />
<input type="text" name="date_bike" value="Bike date" />
<input type="checkbox" name="train" />
<input type="text" name="msg_train" value="Train message" />
<input type="text" name="date_train" value="Train date" />
<input type="checkbox" name="plane" />
<input type="text" name="msg_plane" value="Plane message" />
<input type="text" name="date_plane" value="Plane date" />
Your PHP script:
$array = array("car", "bike", "train", "plane");
for ($i = 0; $i < count($array); $i++) {
if (isset($_POST[$array[$i]])) {
//Checkbox was checked, get values
$msg = "";
$date = "";
$msg_id = "msg_" . $array[$i];
$date_id = "date_" . $array[$i];
if (isset($_POST[$msg_id]))
$msg = $_POST[$msg_id];
if (isset($_POST[$date_id]))
$date = $_POST[$date_id];
}
}
I think something like this should work. I didn't test it.. So forgive me if this example still contains some minor errors.