There is a form with sending data in php and getting the result back.

<form id="call" action=""> <input type="text" name="name"></div> <input type="submit" class="" value="Отправить"> </form> <div id="calls"/> 

call.js

 jQuery(document).ready(function($) { $("#call").submit(function() { var str = $(this).serialize(); $.ajax( { type: "POST", url: "call.php", data: str, success: function(msg) { elm = document.getElementById('calls'); elm.innerHTML = msg; } } }); return false; }); 

call.php

 $call = 1; echo 'Тут текст'; 

How in js to make check on $ call ?.

  • one
    What does check on $ call mean? - L. Vadim
  • In php $ call is 1, in js we do a check on the value of $ call and if $ call is 1, then let's say we show div $("#test").fadeIn; A form with a name is just an example. You need to check in js itself for the value of $ call - nicolaa variable
  • stackoverflow.com/questions/23740548/… and this answer did not suit you? there is a lot of options to get from js to variables from php code - Oleksandr
  • @Olexander Unfortunately, I do not speak English well, it is very difficult to understand. If I watched correctly, then there js and php are in the same file - nicolaa
  • @nicolaa Check the code, I did it - everything works - L. Vadim

2 answers 2

Php:

 if($call == 1) { echo "call = 1"; } 

Jquery

  jQuery(document).ready(function($) { $("#call").submit(function(e) { var str = $(this).serialize(); $.ajax( { type: "POST", url: "call.php", data: str, success: function(data) { elm = document.getElementById('calls'); var regex = /call = (\d)/g; m = regex.exec(data) if(m[1] == 1) { console.log("yes"); $("#test").fadeIn(); } elm.innerHTML = data; } }) return false; }); }); 
  • It works, but in my cases php displays another random text - nicolaa
  • can you give an example of the text - L. Vadim
  • Here is echo '<img src="'.$response->error->captcha_img.'" alt="Captcha">'; echo '<form method="post">'; echo '<input type="text" name="captcha_key" required>'; echo '<input type="hidden" name="captcha_sid" value="'.$response->error->captcha_sid.'">'; echo '<button type="submit" class="btn btn-primary">Отправить</button>'; echo '</form>'; echo '<img src="'.$response->error->captcha_img.'" alt="Captcha">'; echo '<form method="post">'; echo '<input type="text" name="captcha_key" required>'; echo '<input type="hidden" name="captcha_sid" value="'.$response->error->captcha_sid.'">'; echo '<button type="submit" class="btn btn-primary">Отправить</button>'; echo '</form>'; $response->error->captcha_img and $response->error->captcha_sid random. s - nicolaa
  • why in this file captcha? do not quite understand the logic. obyanist pliz - L. Vadim
  • All the code in question is just an example to make it easier to understand what I need. And this is the script for sending posts to the VKontakte group via api. Here is an example of the code, only the form submission was done via js - nicolaa

Roughly speaking, there are a lot of answers to the CO to the question that interests you, I’ll just throw you links in which you can peek at the options for your specific solution, look here , and also here

why in the same? One of the solutions there is to assign your $ foo in HTML class to some value a la data-value and get the data you need from the js file.

  • That is, in php echo '<span id="res" data-variable="test"></span>'; in js, do a check on document.getElementById("res").getAttribute("data-variable"); ? The problem is that all the information that the php transmits is in fact not visible, by going to the Source code, you can see that the div calls in which the text is displayed. Тут текст from php is empty, although the text is visible on the site. When I bring in php (echo) span id res, js just does not see it, maybe I'm wrong, but it seems to be so) - nicolaa
  • In js, yes, when I add document.getElementById("res").getAttribute("data-variable")‌​; to js document.getElementById("res").getAttribute("data-variable")‌​; it stops working immediately, after clicking on the Send, nothing happens, in fact, because it cannot find the id res - nicolaa
  • and you’ve written it like this, you don’t forget about <span id=\"res\" data-variable=\"test\"></span> Just just checked it out, js is okay - Oleksandr
  • I wrote without \, I tried your version, it still does not work - nicolaa
  • I won't help here ... maybe your js is not connected to my code window.onload = function() { var foo = document.getElementById("some").getAttribute('data-value'); console.log(foo); } window.onload = function() { var foo = document.getElementById("some").getAttribute('data-value'); console.log(foo); } window.onload = function() { var foo = document.getElementById("some").getAttribute('data-value'); console.log(foo); } and <?echo "<div id=\"some\" class=\"some\" data-value=\"thisvalue\">adwdawdawdawdawd</div>";?> outputs thisvalue to the console. - Oleksandr