Reputation: 81
Question
The current script I'm working on has one form to get the location then after that is submitted and confirmed without errors it will allow the next button to be available to the user prior to updating it to the database.
The main reason for this is to update the map, to give the user a chance to change their mind before proceeding.
The problem is, I want to keep the data they've already submitted. By doing this
<div class="col-md-6">
<div class="form-group">
<label>Country</label>
<select class="form-control" name="country" value=<?php if (isset($_POST['locate'])) { echo '"'.$_POST['country'].'"'; } else { echo '""'; } ?> required>
<?php include('includes/country_list.php');?>
</select>
</div>
<div class="form-group">
<label>Address</label>
<div>
<input type="text" value=<?php if (isset($_POST['locate'])) { echo '"'.$_POST['address'].'"'; } else { echo '""'; } ?> class="form-control" data name="address" data-parsley-required="true" placeholder="First line address, postcode, zip code etc.." data-parsley-trigger="change">
</div>
</div>
<div class="form-group">
<label>Site Name</label>
<div>
<input type="text" value=<?php if (isset($_POST['locate'])) { echo '"'.$_POST['site_name'].'"'; } else { echo '""'; } ?> class="form-control" data name="site_name" data-parsley-required="true" placeholder="//example: Fawley Refinery, Cottam Powerstation.." data-parsley-trigger="change">
</div>
</div>
</div>
The problem is, I don't want to touch the country_list.php I want to select a default value in the part. However, what I'm currently using does not work.
Upvotes: 0
Views: 501
Reputation: 7663
The problem is, I don't want to touch the country_list.php I want to select a default value in the part. However, what I'm currently using does not work.
To meet your requirement of not modifying the PHP include, you're going to need JS...Something like the below should work.
NOTE: I'm making the value attribute data-value
to be valid HTML:
<select class="form-control" name="country" data-value=<?php if (isset($_POST['locate'])) { echo '"'.$_POST['country'].'"'; } else { echo '""'; } ?> required>
<?php include('includes/country_list.php');?>
</select>
Then, in JS, with jQuery you could do:
$(document).ready( function() {
var $select = $('select[name=country]'),
value = $select.data('value')
;
$select.val(value);
} );
Upvotes: 0
Reputation: 24383
You don't need to use javascript at all to do it:
<select class="form-control" name="country" required>
<?php include('includes/country_list.php');?>
</select>
Then in country_list.php
you would just have to check if each country equals the value in $_POST['country']
. I don't know how you have formatted it, but I'm guessing you're looping over an array of country names. If not, maybe you should be! Try something like this:
foreach ($countries as $country) {
echo "<option value='$country' ";
if (!empty($_POST['country'] && $_POST['country'] == $country)) {
echo 'selected="selected"';
}
echo ">$country</country>";
}
Upvotes: 2