2 out of 3 values gets to next page, first doesnt - php

as the title says, it sends 2 out of 3 values. Im fairly new at php, html coding and the uses of mysql servers so im not sure what the error is.
Btw my project is to make a "Rent a car" site.
First site should be choose car brand, customer and city (sry for swedish, some of the code is in english)
Site#1
https://gyazo.com/86bd55f87f75950013f2c5f3d7466848
code#1
<form action="carsite.php" method="GET">
<table>
<tr>
<th>1.Välj Model</th>
<td>
<p>
<select value="car">
<option disabled selected value="">Välj Model</option>
<?php foreach($res as $row){?>
<option value="<?php echo htmlspecialchars($row->);?>"><?php echo htmlspecialchars($row->make);?></option><?php
}
?>
</select>
</p>
</td>
</tr>
<tr>
<th>2.Välj Kund</th>
<td>
<p>
<select name="customer">
<option disabled selected value="">Välj Kund</option>
<?php foreach($res2 as $row2){?>
<option value="<?php echo htmlspecialchars($row2->id);?>"><?php echo htmlspecialchars($row2->lname);?> <?php echo htmlspecialchars($row2->fname);?></option><?php
}
?>
</select>
</p>
</td>
</tr>
<tr>
<th>3.Välj Stad</th>
<td>
<p>
<select name="city">
<option disabled selected value="">Välj Stad</option>
<?php foreach($res3 as $row3){?>
<option value="<?php echo htmlspecialchars($row3->id);?>"><?php echo htmlspecialchars($row3->city);?></option><?php
}
?>
</select>
</p>
</td>
</tr>
</table>
<input type="submit" value="Sök">
</form>
in my eyes theres nothing wrong. (it sends the id from the tables in mysql)
but at the next site im getting 2/3 values i sent from first page.
Site#2
Notice: Undefined index: car in F:\xamp\htdocs\Slutproject\carsite.php on line 7
12
(12 there is acctually id(customer id) 1 and id 2(city id))
So my problem is why does it send id 2 and 3 but not first one even though they were copy pasted from first id part (the codes of line).
I have tried sending it with GET so i could see the values uptop, still nothing about the car id.
//Mikael

Your first <select> tag has a typo. It should be <select name="car">.

Related

Redirect to current page PHP pagination

first of all, I'm sorry, I am new in this community also my English is not so good, I hope you guys will understand. The question I want to ask is
Currently I'm doing my directory website project, that display all the data in database in pagination because of the massive data in database. User need to select the the state and cluster, then the website will show the list of result. But all the list must be in pagination. I expected when click the page link, it will take the user to 2nd page and so on.
current website display
But the actual result is, when I click the 2nd page, it shows nothing. Like this: the website page after click 2nd page
I am still new in PHP code. I've tried everything I could. I would like to ask, is there something wrong in my coding?
here is some of my coding for "index.php"
enter code here
<form method="post">
<div class="input">
<select name="state" class="btn" required>
<option value="" class="placeholder" selected disabled>Carian Mengikut Negeri</option>
<option value="Johor">Johor</option>
<option value="Kedah">Kedah</option>
<option value="Kelantan">Kelantan</option>
<option value="Melaka">Melaka</option>
<option value="Negeri Sembilan">Negeri Sembilan</option>
<option value="Pahang">Pahang</option>
<option value="Penang">Penang</option>
<option value="Perak">Perak</option>
<option value="Perlis">Perlis</option>
<option value="Sabah">Sabah</option>
<option value="Sarawak">Sarawak</option>
<option value="Selangor">Selangor</option>
<option value="Terengganu">Terengganu</option>
<option value="Wilayah Persekutuan">Wilayah Persekutuan</option>
</select>
<select name="cluster" class="btn" required>
<option value="">Kluster Perniagaan</option>
<option value="Makanan dan Minuman">Makanan & Minuman</option>
<option value="Gaya Hidup">Gaya Hidup</option>
<option value="Automotif">Automotif</option>
<option value="Pembinaan">Pembinaan</option>
<option value="Perkhidmatan">Perkhidmatan</option>
</select>
<input type="submit" name="submit" value="Cari" >
</div>
</form>
<div class="output">
<?php
include "connect.php";
?>
</div>
And here is my "connect.php" that has a query (sorry, i cant show the full code at code sample, so i use snippet)
<?php
$conn = new mysqli('localhost','root','','bpuvirtu_Directory') or die("Connection Failed");
if(isset($_POST["submit"]))
{
$result_per_page = 10;
$str= $_POST["state"];
$cluster=$_POST["cluster"];
$sth="SELECT * FROM TABLE3 WHERE state='$str' and service='$cluster'";
$result=mysqli_query($conn,$sth);
$number_of_results=mysqli_num_rows($result);
$number_of_results;
$number_of_pages= ceil($number_of_results/$result_per_page);
if(!isset($_GET['page'])){
$page=1;
}
else{
$page=$_GET['page'];
}
$this_page_first_result = ($page-1)*$result_per_page;
$sth="SELECT * FROM TABLE3 WHERE state='$str' and service='$cluster' LIMIT " . $this_page_first_result . "," . $result_per_page;
$result=mysqli_query($conn,$sth);
?>
<table id="user">
<tr>
<th>Nama Syarikat</th>
<th><center>Kluster</center></th>
<th><center>Info</center></th>
</tr> <?php
while($row=mysqli_fetch_array($result)){
?>
<tr>
<form method="GET" action=detail.php>
<td><?php echo $row["name"]; ?></center> </td>
<td><center><?php echo $row["service"]; ?></center> </td>
<input type='hidden' name='id' value='<?php echo $row["id"]; ?>'/>
<td><center><input type="submit" name="detail" value="Info" ></center></td>
</form>
</tr>
<?php
}?></table><?php
for($page=1;$page<=$number_of_pages;$page++){
echo '' . $page . '';
}
}
?>
So, when I click page 2, I want it show the list from database at page 2.
You can try this:
//$page = grab page number from the query param here.
return redirect('currencies?page='.$page);
or:
$results->url($page)

Value attribute in the option tag is set to 0 even if I add another value

I have a small exercise to write for a uni class.
I wrote this code as a part of a form
<tr>
<td>Payment Type:</td>
<td><select Name="Payment" form="myform">
<option value="" selected></option>
<option value="0">Monthly Payments</option>
<option value="1">Daily Payments</option>
</select>
</td>
</tr>
and when I try to echo the value of the "Payment" on my PHP file, it only shows 0, even if I select Daily Payments. I tried echo on other values and they work just fine.

select box value changes on form reloading with php validation

I need a solution for this. Here I want to validate form elements. I did it with PHP but what my problem is on clicking submit button validation process takes place but select box value changes to select dept.
FOR EXAMPLE, if you just click submit button without giving student id by only select dept.. the message shows enter studentid but select box value changes to select dept again as it page reloads first..
php:
if($_POST['submit']!='')
{
$c=0;
if($_POST['studid']=='' ) {
$msg_id="Enter stud id";
$c++;
}
if($_POST[studdept]=='') {
$msg_dept="Enter stud dept";
$c++;
}
if($c==0) {
echo "form has been submitted..";
}
}
HTML:
<form id="myform" action="" method="post">
<table>
<tr>
<td> Studid: *</td> <td> <input type="text" name="studid" maxlength="10" value="<?=$_POST[studid]?>"> </td> <td> <?php echo $msg_id; ?> </td> </tr>
<tr>
<td> StudDept: *</td>
<td>
<select name="studdept">
<option value="" selected="selected" >select dept</option>
<option value="cse" >cse</option>
<option value="eee" >eee</option>
<option value="mech" >mech</option>
<option value="ece" >ece</option>
</select>
</td>
<td><?php echo $msg_dept; ?> </td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"> </td>
</tr>
</table>
</form>
can someone help me here?
Thanks in Advance...
You have to generate your <option></option> list through loop (if you're generating Department list from Database Table) where you have to put condition which you had put it for first <option> pair.
Or You can put a Turnary Operator (if you're using static list).
<select name="studdept">
<option value="none" <?php echo ($_REQUEST['studdept']=='none'?'selected="selected"'):'';?> >select dept</option>
<option value="cse" <?php echo ($_REQUEST['studdept']=='cse'?'selected="selected"'):'';?> >cse</option>
<option value="eee" <?php echo ($_REQUEST['studdept']=='eee'?'selected="selected"'):'';?> >eee</option>
<option value="mech" <?php echo ($_REQUEST['studdept']=='mech'?'selected="selected"'):'';?> >mech</option>
<option value="ece" <?php echo ($_REQUEST['studdept']=='ece'?'selected="selected"');:''?> >ece</option>
</select>
And use $_REQUEST instead $_GET or $_POST.
The method of your form is POST and you are trying to get the department using GET <? echo ($_GET['studdept'] == 'cse' ? 'selected="selected"' : '') ?>.
Anyway, you'll need to improve your HTML so you check if any of the deptartments are selected (not just 'cse'). A for/while loop will do the job.
In addition, I would add " " to the post variables, as you did in if($_POST['studid']=='' ) (but you didn't in if($_POST[studdept]==''))
Your specifies that the form data will be submitted using the POST method.
The way that PHP does this is to store all the "posted" values into an associative array called "$_POST".
Be sure to take notice the names of the form data names, as they represent the keys in the "$_POST" associative array.
Use: $_POST['studdept'] instead of: $_GET['studdept']
Beacause you use method POST for send data and you are trying to get value using $_GET['studdept'].
so you have two ways:
(1) just change the method to GET or
(2) change $_GET['studdept'] to $_POST['studdept'].

Remove select tag with text input if user select the certain option which cascade that

I want to make that if user is select certain option(provided from database) only one value then the other option tag appear to select else for all rest of other options user have text box to enter the value. Something like the below-
<tr>
<td><span class="alert">* </span>Country:</td>
<td><select name="country_id" class="medforminput" id="country_id" onchange="getState('select_state.php?country_id='+this.value)">
<option value="Select Country">Select Country</option>
<?php
while($row4=mysql_fetch_array($coun1))
{ ?>
<option value="<?php echo $row4['country_id']; ?>">
<?php echo $row4['country_name']; ?></option>
<?php }?>
<option value="others">Others</option>
</select></td>
</tr>
<tr>
<td><span class="alert">* </span>State</td>
<?php
if($row4['country_name']=="INDIA")
{?>
<td id="statediv"><select name="state_id" class="medforminput" id="state_id" onchange="getCity('select_city.php?state_id='+this.value)">
<option value="">Choose State</option>
</select></td>
<?php else {
<td id="statediv"><input type="text" name="state_id" class="medforminput" id="state_id"/>
</td>
<?php }?>
</tr>
I am getting nothing what wrong in the concept?
I want to display select tag where user choose the "INDIA" else text box to enter the state.
You need to write an onChange event handler in javascript that tests the option selected, then either displays the dropdown or textbox in a dynamic div.
I'm going to use jQuery because it's a LOT less code.
Example:
<select name="country_id" class="medforminput" id="country_id" >
<!-- options go here -->
</select>
<script language="javascript">
$("#country_id").change(function() {
if ($(this).val() == 'India') { // put the country id here instead of string comparison
$("#stateInput").html(getStates());
} else {
$("#stateInput").html("<input type='text' id='stateId' name='stateId' />");
}
});
</script>
Please excuse the pseudocode - I didn't want to make it excessively long.

Submit a dynamic table form through AJAX and get relatives values into PHP

I have a table form like this:
<table class="table table-striped table-bordered table-condensed">
<tbody>
<tr><td><input type="text" class="input-small" name="article" /></td>
<td>
<select name="colore">
<option value="nabuk">Nabuk</option>
<option value="nero">Nero</option>
<option value="blu">Blu</option>
<option value="rosso">Rosso</option>
</select>
</td>
<td>
<select name="fondo">
<option value="gomma">Gomma</option>
<option value="cuoio">Cuoio</option>
<option value="legno">Legno</option>
</select>
</td>
<td>
<select name="numero">
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
</select></td>
<td><input type="number" class="input-mini" min="1" max="200" name="qnt" step="1" /></td>
<td></td>
</tr>
</tbody>
</table>
The user can add many rows to this table with a Jquery script. This script is OK and work properly.
After that, the table form is submitted through an Ajax post call to a PHP page with the $('form').serialize(); function of Jquery.
The problem is: how can I retrieve the values of each field of rows to the server side (PHP), without using hidden supplementary field (for example that submits the number of row in the table) or without use a progressive index for the name of the rows fields?
I haven't found something that explains the solution... :|
the next step of this script is passing the values of the table to the PHP with JSON object... but it's a secondary problem :)
You can use an array. Name your fields with [] at the end of them. For example,
<select name="colore[]">
...
<select name="fondo[]">
...
<select name="numero[]">
PHP will automatically update the indices as
$_POST['colore'][0]
$_POST['colore'][1]
$_POST['colore'][2]
etc.

Categories