How to change form submission into SEO friendly URL - php

I have a simple form that compares two products using a select form and when user hits submit, it redirects to another site but the problem is the URL is rather long and ugly looking right now its displayed like this:
product_details.php?item1=core-i3-7300&submit=Compare&item2=core-i5-12400
I would like the form to submit in this URL structure:
product/item1/item2
Here is the code for my form, I also already have the HTACCESS rule created to accept the URL structure I want, I just need to correct format from the submission form.
<form action="compare/" method="GET" onSubmit="return validateThisFrom (this);" >
<select id="selectPlatform" Name="item1" required="required" class="classic">
<option value="">- Select Product -</option>
<?php foreach ($Names as $Name) : ?>
<option value="<?php echo $Name->Slug; ?>"><?php echo $Name->Make; ?> - <?php echo $Name->Name; ?></option>
<?php endforeach; ?>
</select>
<button input type="submit" name="submit" value="Compare" class="button red">
<select id="selectPlatform2" Name="item2" required="required" class="classic">
<option value="">- Select Product -</option>
<?php foreach ($Names as $Name) : ?>
<option value="<?php echo $Name->Slug; ?>"><?php echo $Name->Make; ?> - <?php echo $Name->Name; ?></option>
<?php endforeach; ?>
</select>
</form>

Related

How to disable input in datalist?

I want to block the user to input I just want him to choose only from the list
I tried disable and readonly but this will disable my list and I don't want that.
This is my code
<div class="form-group row justify-content-center" id=" input-champs " style="margin-left:;" ><!-- grossistee -->
<label for="grossiste" class="col-2 col-form-label"></label>
<div class="col-3">
<input type="text" list="list-gro" placeholder="Grossiste1" id="g_name1" name="g_name1" class="form-control">
<datalist id="list-gro">
<?php while($row = mysqli_fetch_array($resultg)) { ?>
<option value="<?php echo $row['g_name']; ?>"><?php echo $row['g_name']; ?></option>
<?php } ?>
</datalist>
</div>
From the living standard spec of the datalist element:
Each option element that is a descendant of the datalist element, that is not disabled, and whose value is a string that isn't the empty string, represents a suggestion. Each suggestion has a value and a label.
Conversely, this means that a user can enter anything he wants. The datalist element only contains suggestions that the user can use, but does not have to.
For your puprose I 'd suggest a select element. A select element dictates strict values for the user to use.
<label for="my-select">My Select</label>
<select name="my-select" id="my-select" required>
<option value="" disabled selected>Select your option</option>
<?php foreach ($databaseresult as $row): ?>
<option value="<?= htmlspecialchars($row['value']) ?>">
<?= htmlspecialchars($row['label']) ?>
</option>
<?php endforeach ?>
</select>
Its better to use select in place of input as you dont want the user to type in anything from himself/herself.
You can do like this to implement select with database query
<select name="g_name1" id="g_name1" class="form-control">
<?php while($row = mysqli_fetch_array($resultg)) { ?>
<option value="<?php echo $row['g_name']; ?>"><?php echo $row['g_name']; ?></option>
<?php } ?>
</select>

retain select option value in php

<select class="form-control" id="oid">
<option>--Select--</option>
<?php
while($row=mysqli_fetch_array($run))
{
?>
<option value="<?= $row['id'];?>">
<?= $row['first_name'];?> #<?= $row['mobileno']; ?>
</option>
<?php
}
?>
</select>
<?php
echo $row['id'];
?>
i want to echo this select option value in php , it will catch easily in javascript but cant echo in php
it will catch easily in javascript but cant echo in php
PHP is a server-side scripting language, which means that in your case you could only echo the option value of the selection after submitting it to the server. Here echo $row['id']; will just print the last id fetched from your database.
(Client-side) JS executes in the browser, this allows you to track user selections as they are happening.
<form action="#" method="post">
<select class="form-control" id="oid" name="userselection">
<option>--Select--</option>
<?php
while($row=mysqli_fetch_array($run))
{
?>
<option value="<?= $row['id'];?>">
<?= $row['first_name'];?> #<?= $row['mobileno']; ?>
</option>
<?php
}
?>
</select>
<input type="submit" name="submit" value="Submit selection" />
</form>
<?php
if(isset($_POST['submit'])){
echo $_POST['userselection'];
}
?>

show error in same page for the form submission without using session and get

I have a form with three select input.if user doesnot select any of option i want to show error in same page. the user has to insert any of the three select input, user can select in all the input or just one or just two or all. if not i want to show error.
my code:
<form action="booking.php" method="POST">
<h1>SINGLE ROOM</h1>
Number of Rooms:
<select name="singleroom">
<option selected="true" disabled="disabled">0</option>
<?php
foreach($singleroom as $room):
for($i=1;$i<=$room['maxRoom'];$i++){ ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php }
endforeach;
?>
</select>
<h1>DOUBLE ROOM</h1>
Number of Rooms:
<select name="doubleroom">
<option selected="true" disabled="disabled">0</option>
<?php
foreach($doubleroom as $room):
for($i=1;$i<=$room['maxRoom'];$i++){ ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php }
endforeach;
?>
</select>
<h1>TRIPLE ROOM</h1>
Number of Rooms:
<select name="tripleroom">
<option selected="true" disabled="disabled">0</option>
<?php
foreach($tripleroom as $room):
for($i=1;$i<=$room['maxRoom'];$i++){ ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php }
endforeach;
?>
</select>
<input type="submit" name="submit">
</form>
in booking.php i have verified if user has completed the requirement. if not i want to redirect to form page with error.
I dont want to use get and session since it brought some errors.
In your booking.php add the follow code at start where you get form data.
if(!isset($_POST['singleroom']) && !isset($_POST['doubleroom']) && !isset($_POST['tripleroom'])){
header("Location: your_form_page.php?error=1"); // redirect back to your page you are showing form.
}
In your form page where you show the form use this:
if(isset($_GET['error']) && $_GET['error'] == 1){
echo "Please select atleast one field"; // your error message you want to show
}

Set_select() not working on CodeIgniter

I have a select box in my form and when I hit submit the form goes through validation and if an error happens, return all the value. This work expect for my select box, I have use the set_select but it doesnt work data comes in data base
<select name="leave_category_id" class="form-control" onchange="check_leave_category(this.value)" required >
<option value="" >Select Leave Category...</option>
<?php foreach ($all_leave_category as $v_category) :
?>
<option value="<?php echo $v_category->leave_category_id ?>" >
<?php echo set_select('leave_category_id',$cat); ?>
<?php echo $v_category->category ?> </option>
<?php endforeach;
?>
</select>
The set_select() function prints out the selected="selected" attribute in the <option> element where appropriate. But your code is as follows:
<option value="<?php echo $v_category->leave_category_id ?>" >
^
closing the tag --> |
<?php echo set_select('leave_category_id',$cat); ?>
<?php echo $v_category->category ?>
</option>
So when you look at the html, it will look something like this:
<option value="123">selected="selected" Category Name</option>
Got it? Your selected attribute is placed outside the tag. I guess you'll be able to fix it now ;)

Previous dropdown value not getting set with set_value in Codeigniter

The following is my HTML code
<div class="col-md-6">
<label for="martial_status">Martial Status</label>
<select name="martial_status" id="martial_status" class="form-control" value="<?php echo set_value('martial_status'); ?>">
<option value="">Select Martial Status</option>
<?php foreach ($martial_status as $status) { ?>
<option value="<?php echo $status['id'] ?>"><?php echo $status['status_name'] ?></option>
<?php } ?>
</select>
Even though i have used the form_validation in controller side, the value is not getting set in drop down once the form fails submitting. How to set the previous value selected in drop down on form failure. Where am I going wrong.
You cannot set the value straightly to select tag. You need to set the selected attribute to option tag which you want it to be get selected. Just like it below....
<div class="col-md-6">
<label for="martial_status">Martial Status</label>
<select name="martial_status" id="martial_status" class="form-control" value="<?php echo set_value('martial_status'); ?>">
<option value="">Select Martial Status</option>
<?php foreach ($martial_status as $status) { ?>
<option value="<?php echo $status['id'] ?>" <?php if ($status['id'] == set_value('martial_status')) echo "selected = 'selected'"?>><?php echo $status['status_name'] ?></option>
<?php } ?>
</select>
On generating the options tag check which option got selected and echo selected = 'selected' in the attribute of that option tag.

Categories