是使用ajax处理发布变量的好方法吗?

时间:2017-12-16 18:07:52

标签: javascript php jquery ajax

我正在尝试post标题中有ajax的伪,我想我的代码实际上没有向我的php文件发送信息。

所以这是代码:

$('#send').click(function(){
     var pseudo = $('#psd').val();
});

function recuppoint() {
    $.post('point.php',{pseudo: pseudo} ,function(data) {
         console.log('['+data + ']');
         $('.infos2').html(data);
    });
}
setInterval(recuppoint, 1000);	

   
	   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" id="logForm" action="">
    <div class="field">
    	<label class="field-label" for="mailconnect">Votre mail</label>
    	<input class="field-input"type="email" name="mailconnect" id="email" />		
    </div>	
    <div class="field" id="psd">
    	<label class="field-label" for="client">pseudo client</label>
    	<input class="field-input" type="text" name="pseudo" id="pseudo" />
    </div>			
    <div class="field">
    	<label class="field-label" for="mdpconnect">mdp</label>
    	<input class="field-input" type="password" name="mdpconnect" id="password" />	
    </div>	
    <button   id="send" class="send"name="formsubscribe">begin</button>
</form>
    				
<div>
    <div id="infos" class="infos" align="center"> </div>
    <div id="infos2" class="infos2" align="center"> </div>		
</div>

所以,我怎么能用var伪解决问题呢?

谢谢

2 个答案:

答案 0 :(得分:1)

你的id为“psd”的div元素没有价值。如果你想发送html使用.html();如果您只想获得输入值,请尝试以下方法:

var x = $('input[name=nameOfInput]').val();

然后你应该用ajax发送它。

答案 1 :(得分:1)

您的ajax电话应该是这样的。

您无法像input一样使用其父ID访问$('#psd').val();字段 但是您可以使用其本身的

来访问它的值
<input class="field-input" type="text" name="pseudo" id="pseudo" />

$('#send').on('click', function(){
     var pseudo = $('#pseudo').val();
     $.ajax({
          url:'point.php',
          type:'POST',
          data:{ ele: pseudo},
          success:function(data){
              $('.infos2').html(data);
          } 
      }); 
});
   	   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" id="logForm" action="">
    <div class="field">
    	<label class="field-label" for="mailconnect">Votre mail</label>
    	<input class="field-input"type="email" name="mailconnect" id="email" />		
    </div>	
    <div class="field" id="psd">
    	<label class="field-label" for="client">pseudo client</label>
    	<input class="field-input" type="text" name="pseudo" id="pseudo" />
    </div>			
    <div class="field">
    	<label class="field-label" for="mdpconnect">mdp</label>
    	<input class="field-input" type="password" name="mdpconnect" id="password" />	
    </div>	
    <button   id="send" class="send"name="formsubscribe">begin</button>
</form>
    				
<div>
    <div id="infos" class="infos" align="center"> </div>
    <div id="infos2" class="infos2" align="center"> </div>		
</div>