Page 1 of 3
break apart 3 fields that were merged
Posted: Sat Jan 11, 2014 10:42 am
by jaad
HI everyone,
I have a Canadian social insurance input field in a form(actually it is 3 fields composed of 3 number each name=S1[999] name=S2 [999] name=S3 [999]. I've picked up a neath little javascript utility that validate canadian social insurance number on the form. it works great. I was able to merge the 3 fields into one field and save it into a variable $sin and in my table. Now I need to deconstruct this merge so when a user go to their profile they can see their social insurance number back as it was in 3 fields. What would be a method to split apart the series of 9 numbers into 3 chunks of 3 numbers each and assign them to $S1 $S2 and $S3 again?
Code: Select all
$sin1 = $_POST['S1'];
$sin2 = $_POST['S2'];
$sin3 = $_POST['S3'];
$sin = $sin1 . $sin2 . $sin3;
Code: Select all
<input type = "text" id = "S1" name= "S1" size =" 3" maxlength = "3" onkeyup = "validate(this,2)">
<input type = "text" id = "S2" name= "S2" size =" 3" maxlength = "3" onkeyup = "validate(this,3)">
<input type = "text" id = "S3" name= "S3" size =" 3" maxlength = "3" onkeyup = "validate(this,3)">
Also, I think it would be a good thing if I were to pickup the validation boolean from javascript in order not to update the form if the the social insurance number is invalid. I have a javascript button that test the sin nunmber itself but the form itself gets updated with a separate update button at the bottom of the form. How can I talk to the javascript function? do I have to run it again through php somehow to get an internal value that would be evaluated and not echo on the page prior to updating the form? I;m not sure if this makes any sense. English is not my first language. let me know if I should try to rephrase my question
Re: break apart 3 fields that were merged
Posted: Sat Jan 11, 2014 11:01 am
by Celauran
Save it as XXX-XXX-XXX then explode on - to split it back up. As for validation, client-side validation is great, but it can be disabled. I'd add some server-side validation as well. Finally, to remove the need for a button to trigger the JS validation, you could attach a listener to the third of three SIN inputs which will validate the whole SIN on keyup. You could even disable the submit button until this validation has passed.
Re: break apart 3 fields that were merged
Posted: Sat Jan 11, 2014 11:08 am
by jaad
like this you mean: for saving it?
Code: Select all
$sin = $sin1 ."-". $sin2 ."-". $sin3;
and then explode it like this?
Code: Select all
$sin = $S1,$S2,$S3;
$sin explode("-"$S1,$S2,$S3);
Re: break apart 3 fields that were merged
Posted: Sat Jan 11, 2014 11:15 am
by Celauran
jaad wrote:like this you mean: for saving it?
Code: Select all
$sin = $sin1 ."-". $sin2 ."-". $sin3;
Yes, exactly.
jaad wrote:and then explode it like this?
Code: Select all
$sin = $S1,$S2,$S3;
$sin explode("-"$S1,$S2,$S3);
Assuming that $sin is the complete 11 character string, then
Re: break apart 3 fields that were merged
Posted: Sat Jan 11, 2014 11:26 am
by jaad
what is the ( _a ) after the $sin variable? is is some sort of switch?
I know it might sound silly but I haven't slept in roughly 40 hours my mind is not as fresh as can be. where do I insert the explode command line? in one of my function in User Class? lol
Re: break apart 3 fields that were merged
Posted: Sat Jan 11, 2014 11:39 am
by Celauran
It's just a variable name. It has no special significance. Since $sin is the string, I used $sin_a to represent that string split into an array. If you're using explode to get the SIN back into a form for editing, then you can call it while you're building the form.
Re: break apart 3 fields that were merged
Posted: Sat Jan 11, 2014 3:07 pm
by jaad
ok I had a couple hours of sleep, time to get back at it...
So I wrote:
$sin_u explode("-",$sin) and put $sin_u into the function call that updates the user profile and didn't see anything happening nor did I get any errors. I feel there is something missing. It would make sense to me if somehow I would define where the exploded values from $sin_u go on the form fieldS1, fieldS2, fieldS3. I;m not exactly sure how to put them back in there though
EDIT______________-
ok I have tried to build this as a prototype:
Code: Select all
<?php
$sin1 = 123;
$sin2 = 456;
$sin3 = 789;
$sin = $sin1 ."-". $sin2 ."-". $sin3;
//echo $sin;
$s =explode("-",$sin);
$sin1=$s['0'];
$sin2=$s['1'];
$sin3=$s['2'];
//echo $sin1;
//echo $sin2;
//echo $sin3;
?>
<form>
<input type = "text" size="3" value= "<?php echo $sin1;?>">
<input type = "text" size="3" value= "<?php echo $sin2;?>">
<input type = "text" size="3" value= "<?php echo $sin3;?>">
</form>
I'm able to explode and then reintegrate the 3 values from the array back into the input field. The problem that I have right now is this, see form below. I only tried to fill-in the first one to see if it works first and it doesn't. I know I am getting close though. Got to tweak it a bit.
Code: Select all
<input type = "text" id = "S1" name= "S1" size =" 3" maxlength = "3" onkeyup = "validate(this,2)" value="<?php if (isset($_POST['$sin1']) ){echo htmlentities(strip_tags($_POST['$sin1']));} else { echo $s['0']; }?>">>
<input type = "text" id = "S2" name= "S2" size =" 3" maxlength = "3" onkeyup = "validate(this,3)">
<input type = "text" id = "S3" name= "S3" size =" 3" maxlength = "3" onkeyup = "validate(this,3)">
Re: break apart 3 fields that were merged
Posted: Sat Jan 11, 2014 6:37 pm
by Celauran
I only tried to fill-in the first one to see if it works first and it doesn't.
When you say it doesn't work, what happens? Have you checked the values of $_POST and $s? Are they what you expected? $_POST['$sin1'] looks like a typo to me; that $ probably shouldn't be there.
As an aside, you can shorten that echo statement considerably by using a ternary operator.
Code: Select all
<?= isset($_POST['sin1']) ? $_POST['sin1'] : $s[0]; ?>
Re: break apart 3 fields that were merged
Posted: Sat Jan 11, 2014 7:16 pm
by jaad
hahaha well I am getting a tiny little error inside the input text telling me that I have an error on line 259 I can only see 259 in the field because it is size=3. If I would have been a bit intelligent I could have made the size of the field bigger to read the error.... I will go do that and get back to you on that. as for the [$sin1] from what I understand of reintegrating values into the 3 input field that compose the whole social insurance number I have saved the exploded array and save each element in a distinct variable since I have three field to fill as in like my prototype example. What I have problem wrapping my head around is the original values that I type in S1, S2 and S3 and concatenated into one variable $sin and stored inside my database. The way I look at it I can't use $sin as a variable to reintegrate the values back into the input fields because it somehow doesn't' make any sense even if I don't understand why at this point, my gut feeling I guess. So now, in my user class where the update query is happening. I only have and update for $sin. Should I also include $sin1, $sin2, $sin3?
EDIT_____________________________
The error I am getting is: <br /><b>Notice</b>: Undefined index: $sin11 in <b>C:\xampp\htdocs\lar\settings.php</b> on line <b>259</b><br />
The line in my form where I tried inserting the uncooperative variable lol
Code: Select all
public function update_user($first_name, $middle_name, $last_name, $gender, $dob, $sin, $bio, $image_location, $id){
$query = $this->db->prepare("UPDATE `users` SET
`first_name` = ?,
`middle_name` = ?,
`last_name` = ?,
`gender` = ?,
`dob` = ?,
`sin` = ?,
`bio` = ?,
`image_location`= ?
WHERE `id` = ?
");
$query->bindValue(1, $first_name);
$query->bindValue(2, $middle_name);
$query->bindValue(3, $last_name);
$query->bindValue(4, $gender);
$query->bindValue(5, $dob);
$query->bindValue(6, $sin);
$query->bindValue(7, $bio);
$query->bindValue(8, $image_location);
$query->bindValue(9, $id);
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}
and the function call? do I pass the $sin1,2,3 in there too?
Code: Select all
$users->update_user($first_name, $middle_name, $last_name, $gender, $dob, $sin, $bio, $image_location, $user_id);
header('Location: settings.php?success');
exit();
you know, I'm really-really new at this but with your help I am starting to understand concepts that I never did before and I want to thank you for that.
Re: break apart 3 fields that were merged
Posted: Sat Jan 11, 2014 7:28 pm
by Celauran
View source should allow you to see the entire error which, in turn, should give you some insight into what exactly is amiss.
$_POST['$sin1'] is certainly going to be a problem if for no other reason than variables aren't parsed inside single quotes, so it will try to find the key $sin1 in the $_POST array, which doesn't exist. The array keys in your $_POST array will match the name values of the form fields submitted so, in the case of your form above, you'd be looking for $_POST['S1']. $s[0] would be the first element in the array $s created by exploding the $sin value from the database.
What I have problem wrapping my head around is the original values that I type in S1, S2 and S3 and concatenated into one variable $sin and stored inside my database. The way I look at it I can't use $sin as a variable to reintegrate the values back into the input fields because it somehow doesn't' make any sense
You may or may not be right about that. Depends on what other code exists on the page.
Re: break apart 3 fields that were merged
Posted: Sat Jan 11, 2014 7:41 pm
by jaad
his is the error that I get:
<b>Notice</b>: Undefined index: $sin11 in <b>C:\xampp\htdocs\lar\settings.php</b> on line <b>259</b>
which is my line of course.
Code: Select all
<input type = "text" id = "S1" name= "S1" size =" 100" maxlength = "3" onkeyup = "validate(this,2)" value="<?php if (isset($_POST['$sin11']) ){echo htmlentities(strip_tags($_POST['$sin11']));} else { echo $user['$sin11']; }?>">
I think this is beyond my level of understanding at this time.
Re: break apart 3 fields that were merged
Posted: Sat Jan 11, 2014 7:46 pm
by jaad
wouldn't be easier to simply insert the social insurance number in 3 different field in my database and give them a unique variable name? I won't be able to validate the number from the server side with that but at least I can make it work half ass maybe?
Re: break apart 3 fields that were merged
Posted: Sun Jan 12, 2014 3:24 am
by social_experiment
<b>Notice</b>: Undefined index: $sin11 in <b>C:\xampp\htdocs\lar\settings.php</b> on line <b>259</b>
When you receive a notice like this it means that the field you are want to use ($_POST['$sin11'] in this case) isn't present in the $_POST array i.e it isn't anywhere within the form that you submitted.
jaad wrote:wouldn't be easier to simply insert the social insurance number in 3 different field in my database and give them a unique variable name? I won't be able to validate the number from the server side with that but at least I can make it work half ass maybe?
it might be easier but you are leaving yourself vulnerable to any attacks. Granted this script might never function in a setup where it can be accessed by the outside world but i think this approach is going to stick with you in your programming career, short-cuts save time but that's about the only good thing about them in programming.
Re: break apart 3 fields that were merged
Posted: Sun Jan 12, 2014 4:03 am
by jaad
thanks for checking this out for me. I definitely prefer not going half ass on this. Proof is that I have been working for about 4 days to get two fields DOB and now SIN to get validated and process the right way. I am certainly not lazy but at this stage I fear my experience and knowledge is not enough to get me to solve this. I have no idea how I can reintegrate the concatanated database value back into the 3 original field this value comes from.
I built a protype exercise with static values in a test page and I was able to explode the value and reintegrate the array back into the 3 field but It doesn't when I go into the real system with a database. Im a green horn and that doesn't help lol
Re: break apart 3 fields that were merged
Posted: Sun Jan 12, 2014 4:16 am
by social_experiment
jaad wrote: I have no idea how I can reintegrate the concatanated database value back into the 3 original field this value comes from
from the code already created you're half-way there; you have the code that breaks the value into 3 pieces, you've written it to the db (yes / no ?) and putting it back into a form isn't that difficult; it's basically what you did in step one
1. Retrieve data from DB
2. Explode it
3. Put it in 3 text fields