Doubts Date with PHP and JS

0

I do not know anything about JS and would like to know if I have edit a field whose value came from the database using javascript? Because I saved the date of birth and the phone number of a client on the bank, and at the time of registration I saved the phone without the mask and date in the Year-Month-Day pattern.

I would like to know if you can use js to format these values from the bank so that it displays like this (99) 99999-9999 and date 99/99/9999.

Note: I just asked this question because I could not find anything on google.

    
asked by anonymous 03.03.2016 / 06:30

2 answers

3

What you are looking for are regular expressions, through which you can find and modify parts of strings:

Exemplifying: I have the value of the bank printed on the page:

<p class="phone">1199999999</p>

Modifying the text inside the p-tag:

 $(".phone").text(function(i, text) {
    text = text.replace(/(\d{2})(\d{4})(\d{4})/, "($1) $2-$3");
    return text;
});

Example online in JsFiddle: link

More about regular expressions in javascript: link

    
03.03.2016 / 08:32
1

Generic formatting with dynamic mask:

function format_string(mask, str, ch) {
	ch = (typeof ch === "undefined") ? "#" : ch;
    var c = 0, r = "", l = mask.length;
    for (i = 0; i < l; i++) r += ((mask.substring(i, (i+1)) == ch) ? str.substring(c, ++c) : mask.substring(i, (i+1))); 
    return r;
}

str = '05055344410'; // Exemplo para telefone
document.write(format_string('###-####-####', str));

str = '20150827'; // Exemplo para datas
document.write("<br />"+format_string('####-##-##', str));

str = '100000'; // Exemplo para horário
document.write("<br />"+format_string('##:##:##', str));

str = '45564646456'; // Exemplo para cpf
document.write("<br />"+format_string('###.###.###-##', str));

str = '85282224448'; // Exemplo para qualquer outro formato, um telefone com código do país.
document.write("<br />"+format_string('+### ####-####', str));

The routine is based on another response with PHP. If you are interested in writing in PHP, read the post: link

Note that you do not need libraries or plugins.

    
03.03.2016 / 12:08
Get function in service.js returns null ___ ___ erkimt Get database data ______ qstntxt ___

Well it's the following I have the following code:

%pre%

Supposedly, I want to get the login data, where login is equal to $ login variable, and then display the login password, however the following errors appear:

  

Warning: mysqli_query () expects at least 2 parameters, 1 given in   C: \ Program Files \ VertrigoServ \ www \ adm \ logar.php on line 9

     

Warning: mysql_fetch_array () expects parameter 1 to be resource, null   given in C: \ Program Files \ VertrigoServ \ www \ adm \ logar.php on line 10

What's wrong?

Thank you.

    
______ azszpr115525 ___

The %code% function in procedural mode waits for a parameter variable to create a connection with the bank and the one with the query itself, you are just passing the query.

Try this:

%pre%

However, the query would be very vulnerable in this way, you could parameterize this query, like this:

%pre%

This narrows the vulnerability a bit, but does not mean that the code is safe with just that.

    
______ azszpr115524 ___

It is necessary to pass the connection parameters when using the mysqli_query functions, and as the comrade said, try not to mix the two libraries.

More about mysqli: link

    
___