PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 15, 2022

[FIXED] Extract the value of field in php and save

 February 15, 2022     php, yii     No comments   

Issue

Hello I have a textArea sms and another textfield sms_count..

The value of sms_count is like this Used: 0/765 (1/5) | Left: 765 Whenever user type anything in textArea sms it counts the characters and shows it in sms_count

if user has typed 30 characters so far, so the value of sms_count will be like this Used: 30/765 (1/5) | Left: 765

When clicking on submit button, I get the value of sms_count like this..

sms_count => Used: 30/765 (1/5) | Left: 735

How do i get the characters that he entered which is 30, I need to save the value 1 in (1/5) only instead of Used: 30/765 (1/5) | Left: 748

I need to get only the used value which is 1 in this case but it can be different... if user type more than 160 characters then sms_count value will be 2 (2/5) then again if user has enter more than 320 characters then value be 3 in (3/5)

example 30/765 (1/5) | Left: 735 161/765 (2/5) | Left: 604 330/765 (3/5) | Left: 435

So how do i extract those values in save in database through PHP?

Below is the screenshot of two fields one is textArea sms and textfield sms_count

enter image description here

<div class="form-group">
<?PHP echo $form->textArea($model, 'sms', array('class' => 'form-control', 'id'=>'Campaigns_welcome_sms', 'onpaste' => "return textCounter(this.form.Campaigns_welcome_sms,this.form.smsAndCharactersCount,this.form.smsAndCharactersCount);", 'onkeyup'=>"textCounter(this.form.Campaigns_welcome_sms,this.form.smsAndCharactersCount,this.form.smsAndCharactersCount);", 'onkeypress'=>"textCounter(this.form.Campaigns_welcome_sms,this.form.smsAndCharactersCount,this.form.smsAndCharactersCount);")); ?>                        
<?php echo $form->textField($model, 'sms_count', array('class' => 'form-control', 'style'=>"width: 370px", 'id'=>"smsAndCharactersCount", 'value'=>"Used: 0/765 (1/5) | Left: 765", 'readonly'=>"readonly")); ?>
</div>

Solution

With a little bit of string manipulation you can extract the numbers you want like this:

$sms_count = 'Used: 30/765 (1/5) | Left: 735'; // YOUR VARIABLE FROM SUBMITTED DATA 

$sms = explode('/',$sms_count); // SPLIT STRING TO ARRAY BY SLASHES:    

//CREATE VARIABLE $user THAT CONTAINS THE NUMBER OF CHARACTERS USED:
$used = substr($sms[0], stripos($sms[0],' ')+1); //FINDS THE SPACE ' ' AND RETURNS THE REST OF THE STRING STARTING FROM THERE...

//CREATE VARIABLE $count THAT CONTAINS THE NEXT NUMBER YOU WANT:
$count = substr($sms[1], -1, 1); // EXTRACTS THE LAST CHARACTHER/NUMBER

You could use echo 'used: '. $used .' count: '.$count; to check if the results is what you are after. In this case i would output: used: 30 count: 1

How you choose to save this to your database depends on your setup. It's just an ordinary insert or update query to make. A regular SQL insert query would be something like:

$sql = "INSERT INTO table_name (identifier, used, counts)
        VALUES ('$identifier', '$used', '$count')";


Answered By - Michael Krikorev
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing