This may sound confusing but I have a textbox which is a cell number textbox and I want when the max length of 10 has been reached then fire the php code which is inside the same html page as form and this php code will then get the value or data which has been keyed in to this text box. I've tried Javascript and the problem I'm having with javasacript is to now display this data so that why I've deiced to change to php because I know how to display the php session data within the textbox with something like

<input type="text" id="dCell" value="<?php echo $_SESSION['mySessionIDHere'];?>">"

so I tried to do something like this but with javascript but the whole tag codes just show on screen when I run load the page so that why I've choose to go with php.

Please do note that the page is HTML and the input from which I want to take the data from is also in html not php the php function script will be written within the html page so all of this will be in one page not that it will be passed to another page no.

Recommended Answers

All 20 Replies

You could fit what I know about HTML5 in a test tube and have plenty of space left over, so I have nothing useful to say on that tag. BUT...

I know something about your other three tags so I'll chime in anyway. Yes, your question is confusing. At least to me. My instinct here is to not try to solve a Javascript problem with a PHP workaround. PHP is server side. PHP code is parsed into HTML and Javascript code on the server and sent to the user's computer, where a browser like IE or Firefox or Chrome or whatever parses that HTML code and sticks a form on your screen with a text box that you can type things into. You can create a Javascript function that is called when a user types something into that text box. That Javascript function can see what has been typed in and potentially do something based on some criteria (in your case, that criteria would be the text box contents having length ten).

Where I'm confused is where, how, and most importantly WHY, PHP would be doing anything in this process as far as retrieving what has been typed in or doing anything based on that. The PHP part was done the second it finished parsing the PHP code on the SERVER computer. When I "View page source" for daniweb.com or yahoo.com or google.com, I see absolutely no PHP code and I've never seen it anywhere else on a client computer. The client computer would need to have PHP installed and very, very few client computers have that.

the problem I'm having with javasacript is to now display this data so that why I've deiced to change to php because I know how to display the php session data within the textbox with something like

If you want to grab the text typed into the text box WITH JAVASCRIPT, then refresh the page so it sends that text to the sever and PHP reparses and redelivers the page back to the client computer using the new info in the text box, that's doable. But you specifically say you don't want to POST or GET this information. Extracting the text box contents using PHP? Doesn't make sense to me.

My feeling is that this is a Javascript problem and should remain one. Don't try to make up a Javascript deficit with a PHP workaround. Unless I'm missing something.

I agree in that the only way I can see is to submit the form back to the server. Something like:

<!DOCTYPE html>
<html>
<body>
<script>
  function chkLen() {
  if(document.getElementById("dCell").value.length =10)
    document.getElementById("myForm").submit(); 
}
</script>
<form name="myForm" onsubmit="myPhp.php">
<input id="dCell" type="text" onChange="chkLen();" value="<?php echo $_SESSION['mySessionIDHere'];?>" />
</form>
</body>
</html>

I suppose I am hung up on this part...

without POST or GET calls

When the form submits to the server PHP page with the information typed in, a normal form will use "POST" or "GET" (you can specify which to use in the form). The PHP receiving this info will use $_POST or $_GET to grab that information.

The OP apparently does not want to do this for some reason? If I type in "0123456789" into the text box, does "0123456789" get delivered to the server? If so, how?

It still seems as if the OP would rather not submit anything to the server at all but simply does not know how to do what he wants to do in Javascript. To reiterate, I am confused with the goal here.

Hi guys sorry for the late reply.

Basically what I want is to take what is typed on the textbox and assign it to another control variable or at least take that data that was typed and assign it to session with Javascript so that I will be able to keep using that data within some control before the form is actually submited. So the form submit goes to another separate file.

I think triggers like these onChange="chkLen();" would work instead of submitting the form. So I think now whats left is to get a way of, on onChange="chkLen();" get the actual data typed and then take that data and assign it to a session mySessionIDHere so that when I call this session on controls the data that was typed on that textbox will be displayed on the control I'm calling the session on.

I'm not sure if that is clear now but I hope it is.

I want to achieve something like this or I want to do this but its doesn't work here how can I do it, take note of parameter data-name

<div class="clearfix"></div> <button id="cCart" class="btn btn-danger my-cart-btn my-cart-b" data-id="16" data-name="<script>var session ='<%=Session["unumber"]%>'</script>" data-summary="summary 16" data-price="50" data-quantity="1" data-image="images/of15.png">MMM</button>

The texbox which the data will be taken from:

<input  type="text" id="dCell" value="Cellphone Number" maxlength="10" onkeypress="checkmax(this)" name="CellphoneNumber2" id="cCell" onblur="checkmax(this)" onfocus="if (this.value = '') {this.value = 'Cellphone Number';}" required>

The checkmax script:

<script>
                    function checkmax(element){
                        if(element.value.length == 9){
                            //var idNumber = $('#dCell').val();
                            var vvc = document.getElementById('#dCell').value;
                            <%Session["unumber"] = vvc;%>
                            var Session_value='<%=Session["unumber"]%>';
                            alert(Session_value);
                            //$(function() {
                                //$('#dCell').change(function() {

                                    //'<%Session["unumber"] = "' + this.value + '"; %>';
                                    //alert('<%=Session["unumber"] %>');
                                    //<?php
                                    //$thh = (this.value);

//$_SESSION['thulani'] = $thh;
//?>

                                    //$('#cCart').attr('data-name', this.value);
                                //});
                            //});
                        }else{

                        }
                    }
                    </script>

One way could be to assign the value to a hidden input label. For example:

<?php
 $_SESSION['num']='4567890123';
?>

<!DOCTYPE html>

<html>

<body>

<script>

      function checkmax(element) {

          if(element.value.length == 9){

              var vvc = element.value;

              element.value =document.forms['myForm'].elements['h1'].value;

//alert(vvc);
          }
      }

</script>

<form name="myForm" onsubmit="myPhp.php">

<input  type="text" id="dCell" 

    value="Cellphone Number"
    maxlength="10"
    onkeypress="checkmax(this)"
    name="CellphoneNumber2"
    id="cCell"
    onblur="checkmax(this)"
    onfocus="if (this.value = '') {this.value = 'Cellphone Number';}" required>

<input name="h1" type="hidden" value="<?php echo $_SESSION['num'] ?>">
</form>
</body>
</html>

Thanks but I think you miss understood. I want to take what is typed on the dCell input box and assign it to a session so that I can call that session to have that data.

So this

<?php
 $_SESSION['num']='4567890123';
?>

should not be hardcoded but it should be assigned by a value that will be from dCell inputbox so if it possible it this php should be in the checkmax function and it should get the data from there. Basically I want to assign this data to a variable that will be sent as a query in the url when the the user click the button.

The problem with the button is that its a link button so it doesn't submit the form but the link is formed and the user is taken to that link. This is an integration with payment gateway so this data should be passed to them via this way so that why I don't post or get this to another php file.

This is an integration with payment gateway so this data should be passed to them via this way so that why I don't post or get this to another php file.

Really? Which gateway requires this kind of ridiculous gymnastics?

I think that your problem is self-inflicted.

In one or two sentences, what are actually you trying to do?

Well @pty you can call it whatever you call it, but the it remain the same. The reason for you to talk like that is because you haven't faced this kind of gateway so I wont argue with you but it is what you see.

@pty

what are actually you trying to do?

Well I've already stated

what I want is to take what is typed on the textbox and assign it to another control variable or at least take that data that was typed and assign it to session

No, that's how you're currently trying to do it. It being something confusing.

What is the outcome? Simple, a couple of sentences max. For example:

I'm trying to implement some kind of custom phone number validation where numbers follow [this pattern]

or

I'm trying to record someone's phone number without them actually submitting the form

By using words out of context and nonsensical phrases like "assign it to a session" confuses matters to the point where I can't tell what you're actually attempting to write.

Ok lets stick with

I'm trying to record someone's phone number without them actually submitting the form

Then call by means of XMLHttpRequest another php that grabs the phone number and any other data to record what is necessary.

<script>

      function checkmax(element) {

          if(element.value.length == 9){

             var xhr = new XMLHttpRequest();
             xhr.open('POST', '/server/getPhone.php', true);
             //Send the header 
             xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

             xhr.onreadystatechange = function() {//Call a function when the state changes.
             if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
             // Request finished. Do processing here.
             }
             xhr.send("phone="+urlencode(element.value)+"&foo=bar");

          }
      }

</script>

This is a shady tactic at best. If someone doesn't submit the form, calling them to offer your services is likely to make them run a mile and choose a less-scummy competitor. I certainly would.

If you're selling on their data it's even worse. But, you know, it's your call.

commented: +1 this is bad, really... really bad! +7

Mr. M,

There's the technical side and there's the ethical side. I believe that how to extract text box contents and stick it into a variable on the CLIENT SIDE has already been covered in this thread, as has how to submit that form to a server using Javascript without the user pressing a "Submit" button. Or something fairly close. Setting a SESSION variable on the client side using Javascript hasn't really been touched.

The ethical considerations have been laid out above. You and I have had some interaction and my impression of you has not been that you are a spammer or information thief. However, in THIS thread, I can definitely see how someone would have that concern. You are asking something that SEEMS like it should probably be fairly straightforward in a complex manner. Sometimes that legitimately happens when there is an English language barrier or a programming concept barrier (ie a "noob" to a certain language or concept uses the wrong terms or does not know what precisely he/she wants). However, it also happens when someone is up to no good and needs to hide his/her true question. You need to design your questions in a way that no one sticks you in this category.

Case in point. A while back there was this (I assume) kid who kept having problems with his Windows Hooks. If I recall correctly, he was building Piano applications, typing applications, all sorts of applications. What they all had in common was that they all had a global Windows Hook in there for no real reason UNLESS the real reason was that you were actually building a keylogger.

I imagine that's how you're coming across here to some folks. To reiterate, my interactions with you make me think you're trying to do something legitimate. You're working on some stuff where you don't want to divulge your Intellectual Property. Understandable, but I think you don't have to worry as much as you are and can just come right out and say what you are trying to accomplish and why.

@AssertNull, I think the concept Mr.M is working on smells like a bad thing because either they didn't present it well or it is just someone trying to be underhanded and scrape data.

For example, read https://freedom-to-tinker.com/2017/12/27/no-boundaries-for-user-identities-web-trackers-exploit-browser-login-managers/ and just sending the content without post etc could be an attempt to thwart privacy managers.

Most folk here won't help when it comes to such work.

Most folk here won't help when it comes to such work.

Including me. I'm hoping that's not the case here.

Setting a session variable in javascript is not possible. Javascript executes on client side while php (php session variable) executes on server side, i.e. generaly on different machines.

I don't want to interfere to the conversation (since I didn't understood why the thread starter want to do it that way) but because I realized that a hint could help others , I decided to write few lines.

What you can get in PHP (and other languages) through $_SESSION is stored in a file (usually in the tmp folder - but it also could be in a DB) for a limited period of time (usually a robot detects when that file have been accessed last time and a defined time has passed – e.g. 30 minutes – deletes it). So you can't modify it only by client side.

You can have exactly the same in JavaScript through the sessionStorage (HTML5 Web Storage API). Of course that way you can't access the web server session as explained above , and PHP (and any server side language) can't access directly that client side session. But it is a persistent session (name / value pairs) across pages in the same domain that you can access through JavaScript.

If you need something that both JavaScript can access and modify it , and server side PHP for example , this is cookies. Setting a cookie with JavaScript you can get it in the next PHP call without sending it immediately. See for example Click Here for an easier use of cookies with JavaScript. Of course to get it server side without a server request when you set it , it means that the user will make a server call before the cookie expires or delete it.

Mmmm thanks everyone for your comments and your suggestions as well. Well I will close this post. I've got an alternative to this problem I had.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.