I want to add a permanent +234 to the phone number value before it is submitted into the database
I have tried to type (+234).('Alternate_Number) on the form page to see if it will pass the array into the database but it didnt work
<div class="form-group row">
<label class="col-sm-3 col-form-label" for="Alternate_PhoneNumber">Alternate PhoneNumber :</label>
<div class="col-sm-9">
<input type="tel" name="Alternate_PhoneNumber" placeholder=" (08032xxxxxx)" class="Alternate_PhoneNumber form-control" id="Alternate_PhoneNumber" value="<?php echo set_value('Alternate_PhoneNumber', ''); ?>" minlength="11" maxlength="11">
</div>
</div>
controller
$this->form_validation->set_rules('Alternate_PhoneNumber','Alternate PhoneNumber','|numeric|exact_length[11]|xss_clean');
main model
'Alternate_PhoneNumber' => $this->input->post('Alternate_PhoneNumber'),
i want to just have the submitted value in the database to be +234 and the value submitted by the user like +2340834144706
Would this work for your case?
'Alternate_PhoneNumber' => '+234' . $this->input->post('Alternate_PhoneNumber'),
Use concatenation before saving data into database.
1) Suppose the variable $PhoneNumber = '0834144706' is your actual number.
2) And one more prefix variable called $country_code = '+234';
3) Do concatenation like this (Full Code)
$PhoneNumber = '0834144706';
$country_code = '+234';
$final_number = $country_code.$PhoneNumber; // Has value +2340834144706
and save the $final_numbervariable data into database.
Related
i want to ask something, i passed a value from controller to view and in database, i wonder how do i delete the "/" character. here's the code
Dashboard.php
<form action="/dashboard/post" method="post">
<input type="hidden" name="user_id" value=<?=$userid?>/>
<div class="form-group">
<label for="">Username</label>
<input type="text" name="user_name" id="" class="form-control">
</div>
<div class="form-group">
<label for="">Email</label>
<input type="text" name="user_email" id="" class="form-control">
</div>
</form>
Controller.php
public function index()
{
$model = new DataModel();
$user = $model->findAll();
$hidden_data = [
'userid' => count($user)
];
return view('dashboard', $hidden_data);
}
here's the table
honestly i just want to the user_id insert the number only without "/". i knew that sql has auto increment feature but actually i want to create a custom id.
example:
before =
user_id = 10/
after =
user_id = 10
It looks like your input field for user_id includes the slash, so I assume you get the slash in your PHP code as well.
I recommend you to go step-by-step when issues like this occurs. You know the value that is causing issues, so follow it down to where you notice the error and you can usually find the cause. Often you will notice that it's the most simplest of things that are the cause of the issue, but they are also easily overlooked.
Also, since you include the user_id in the form itself you should beware that the user that looks at the page can edit it before submit, so you shouldn't trust that the hidden input is what you set it to be unless you have some sort of validation of it.
I have succesfully retrieved two columns from my table and displayed the result as options in a select tag, in my form.
The problem is I cannot display the data from a third column into an input field, which is also in the form, based on the selected option.
There is no issue with the database connection or retrieving the data for the options in the select tag.
Any help would be appreciated. The relevant code is below.
<div class="col-md-4 col-12 bottommargin-sm">
<label for="">Choose currency</label>
<select class="form-control" id="exampleFormControlSelect1">
<option value="">Choose currency...</option>
<?php
// Including DB connection file
include_once "config/config.php";
// Retrieving all columns from currency table
$result = mysqli_query($con, "SELECT * FROM currency_test");
// Loop printing options with country and currrency for all rows in currency table
while($row = mysqli_fetch_array($result)) {
print "<option>";
echo $row['country'] . " - " . $row['currency'];
print "</option>";
}
// Closing DB connection
mysqli_close($con);
?>
</select>
</div>
<div class="col-md-2 col-12 bottommargin-sm">
<label for="">Currency rate</label>
<input type="text" class="form-control" value="<?php echo $row['buy_rate'] ?>" readonly>
</div>
Additional Edit
I am now using the following code, still without any luck:
<div class="col-md-4 col-12 bottommargin-sm">
<label for="">Choose currency/label>
<select class="form-control" id="exampleFormControlSelect1">
<option value="default">Choose currency...</option>
<?php
// Including DB connection file
include_once "config/config.php";
// Retrieving all columns from currency table
$result = mysqli_query($con, "SELECT * FROM currency_test");
// Loop printing country, currrency, buy_rate and sell_rate for all rows in currency table
while($row = mysqli_fetch_array($result)) {
echo "<option value=".$row['currency']."> ".$row['country']. " - " .$row['currency']." </option>";
}
?>
</select>
</div>
<div class="col-md-2 col-12 bottommargin-sm">
<label for="">Currency rate</label>
<input type="text" class="form-control" value="<?php echo $row['buy_rate'] ?>" readonly>
</div>
<?php
// Closing DB connection
mysqli_close($con);
?>
As said before, you have a result SET in your while-loop above, but you want the input field only to contain a single value: the buy-rate of the selected currency. If I guessed right and this is what you want, this will not work like this.
If you want to test it and just retrieve only a single value in your while-loop, also your input field gets filled with the correct buy-rate.
You can do it with or without JavaScript, but you need a second statement to retrieve the buyrate you desire.
Further security suggestions: You should not use the include directives without __ DIR __ . Neither you should use "SELECT * ...". Better it is to name all the fields you want to retrieve.
You will have to have a statement for that selection. (Hint: Also in the tag you will have to have a value attribute set) Also your select needs a name if you use a submit button..
Now when you submit the form you can query the database and show the correct record.
If you dont want to use a submit button and have the value in the textfield change by selection, you will need javascript.
So what I see :
1) No name in select tag
<select class="form-control" id="exampleFormControlSelect1">
There is no name attribute. So after posting the form, the server doesn't know the name of the variable.
2) No value
print "<option>";
You don't supply a value. So your server doesn't know the value (the user selected) after posting the form.
This is the proper way for a select tag : https://www.w3schools.com/tags/tag_select.asp
I am creating a profile page with forms that displays the user's account information from the database in a form. I am using php for this. Would greatly appreciate your help!
Name:
Email:
How can i edit the values in the forms and retain the edited values in the forms after submission?
<div class="form-group">
<label class="control-label" for="name">Name:</label>
<div class="col-sm-4">
<input type="type" name="name" value="<?php echo $name;?>" class="form-control" placeholder=""><br>
</div>
</div>
<div class="form-group">
<label class="control-label" for="email">Email</label>
<div class="col-sm-4">
<input type="text" name="email" value="<?php echo $email;?>" class="form-control" placeholder=""><br>
</div>
</div>
<?php
$sql = "SELECT * FROM profile WHERE UserID ='1'";
$result = mysqli_query($conn, $sql) ;
$row = mysqli_fetch_array($result);
$name = $row ['Name'];
$userid = $row ['Email'];
?>
if(isset($_POST['submit']))
{
$n1 = $_POST['name'];
$n2 = $_POST['email'];
mysqli_query($conn,"UPDATE userprofile SET Name ='$n1' WHERE UserID ='1'");
mysqli_query($conn,"UPDATE userprofile SET Email ='$n2' WHERE UserID = '1'");
}
how can i edit them and retain the edited values inside after submitting? Thank you. I tried this filter/trim method but it only retains the value before the submission.
Not sure what you want to achieve, and what is your problem.
Let's assume you are displaying data describing user (in form), that are fetched from the database.
In this case, after submitting you have to run script that will update data in your database. After updating, your code should redirect user BACK to page he were visiting before, so fetching user data from database will be done again and data presented in your form will be always fresh.
Your code is correct but it has a small bug. The email field is not displaying correctly. You need to replace this line:
$userid = $row ['Email'];
with
$email = $row ['Email'];
I have a large form where 3 arrays are sent to the controller from the form: product_code | quantity | cost.
$id = $request->get('product_code'); // GRAB PRODUCT DATA FROM POST ARRAY
$qty = $request->get('quantity'); // GRAB QUANTITY DATA FROM POST ARRAY
$cost = $request->get('cost'); // GRAB COST DATA FROM POST
The output from the request on all three arrays is here: PasteBin
My problem is I can not figure out how best to loop through each of these three arrays so that I can insert into MySQL the values in the correct order. I have tried both a foreach, a nested foreach and a for loop and I have not managed yet to get all three values inserting onto a single row.
I don't think the HTML is very relevant, but here is a sample anyway:
<div class="well form-group ">
<div class="col-sm-2 col-md-2">
<label for="nails_staples">Nails & Staples:</label>
</div>
<div class="col-sm-4 col-md-4">
<select class="form-control product_id" name="product_code[10]">
<option selected="selected" value="">None</option>
<option value="8769">1 1/4 Coil Nails - box | $26.00</option>
<option value="6678">2" Hot Dipped Shake Nails | $135.00</option>
</select>
</div>
<div class="col-sm-1 col-md-1">
<label for="nails_req">Quantity:</label>
</div>
<div class="col-sm-2 col-md-2">
<input class="form-control quantity" name="quantity[10]" type="text">
</div>
<div class="col-sm-1 col-md-1">
<label for="cost">Cost:</label>
</div>
<div class="col-sm-2 col-md-2">
<input class="form-control cost" readonly="readonly" name="cost[]" type="text">
</div>
</div>
There are a few issues with this approach. Ideally you'd have separate calls to a controller for each entity you want to change, using AJAX to make each individual call as the user changes the entity. This solves the issue of attributes not being strongly tied to the entity ID (in your case, product_code).
If you're willing to make the assumption that all of the arrays match up, you should at least check that they are the same length.
Obviously I don't know all the specifics of your Model or exactly what you're looking for, but based on your paste bin, this should work.
$id = $request->get('product_code'); // GRAB PRODUCT DATA FROM POST ARRAY
$qty = $request->get('quantity'); // GRAB QUANTITY DATA FROM POST ARRAY
$cost = $request->get('cost'); // GRAB COST DATA FROM POST
$count_ids = count($id);
if(count($qty) != $count_ids || count($cost) != $count_ids) throw new Exception("Bad Request Input Array lengths");
for($i = 0; $i < $count_ids; $i++){
if (empty($id[$i])) continue; // skip all the blank ones
$newProduct = new Product();
$newProduct->code = $id[$i];
$newProduct->qty = $qty[$i];
$newProduct->cost = $cost[$i];
$newProduct->save();
}
In the billing_fields.phtml there is
<?php
$billingFields['country_id'] = '
<div class="input-box input-country'.((in_array('country', $formErrors)) ? ' input-error' : '').'">
<label for="billing:country_id">'.$this->__('Country').' <span class="required">*</span></label><br />
'.$this->getCountryHtmlSelect('billing').'
</div>';
?>
I do have only one country and the select-tag $this->getCountryHtmlSelect('billing') with the rendered dropdown is not user friendly as it has no further options. I feel its misleading and obsolete. My question is:
How do I have to change the code above to show my default country in a simple (not editable) input field?
EDIT
I came up with this after CBroes getCountryCollection hint
<?php
$countryCollection = $this->getCountryCollection();
foreach($countryCollection as $country) {
$country_id = $country['country_id'];
$countryName = Mage::getModel('directory/country')->load($country_id)->getName();
$billingFields['country_id'] = '
<div class="input-box input-country'.((in_array('country', $formErrors)) ? ' input-error' : '').'">
<label for="billing:country_id">'.$this->__('Country').' </label><br />
<input type="text" name="billing[country_id]" id="billing:country_id" class="input-text" value="'.$countryName.'" style="width: 83%" readonly="readonly" />
</div>';
}
?>
The frontend looks fine. Even the backend recognizes the given value. Unfortunately the emails don't. The country field remains empty there.
What am I missing or how do I make this input legit so that magento-emails accept its value?