I Created a button that when onclick he delivering a prompt message in order The user will enter a free text. Now I need to use this free text and pass it as var input with GET method to external PHP in my system. I have tried using the getJSON method to do the following :
'<button onclick="SendFreeMSG()">Test MSG </button>
<p id="testMSG"></p>
<script>
function SendFreeMSG() {
var InputMessage= prompt("Please enter your Message", "Write down your message Here");
if (InputMessage!= null) {
document.getElementById("testMSG").innerHTML = "Your message is: " + InputMessage + " " ;
$.getJSON("sending_msg_to_user.php?userinput=" . $InputMessage ["userinput"] .
}
}
</script>' ;
The scrpit is working fine without the getJSON row, but when I tried it with that row nothing happen.
----- Addition ---- Here is another part of my code with click button and $getJSON In this part it is working well:
'<button onclick="' .
"if (confirm('sending message ?')) {
var activeValue = $(this).siblings('.all_users_active_value');" .
"var activeCaption = $(this).siblings('.all_users_active_caption');" .
"$.getJSON('sending_general_msg.php?General=" . $rowData['General'] .
"&active=' + (activeValue.html() == '1' ? '0' : '1'), " .
"function(data) { " .
"activeValue.html(data.active);" .
"activeCaption.html(data.active == 1 ? 'Active' : 'Inactive')" .
"})
} else {
alert('sending message cancelled')
};" .
"return false;".
'"> Sending new message </button>';
I will appreciate any help with that matter
Abraham
You need to use ajax to send sometime from java to a php file. below is a tutorial on how to use ajax.
If you need to pass the input value to another php, you must to use Ajax in jQuery, I hope this example will help you
$.ajax({
type:"get",
url:"sending_msg_to_user.php",
data:"InputMessage="+InputMessage,
beforeSend: function(){
$('#some_div').html('<img src="img/Loading.gif"/> Sending Data');
},
success:function(data){
alert(data);
$('#some_div').html('');
}
});
If you have an echo in sending_msg_to_user.php you could see in an alert message tha value of inpu in prompt
Your $.getJSON request is poorly formed. If you paste that function directly into your favorite JavaScript console and hit 'enter', you would see the error:
Uncaught SyntaxError: Unexpected token }
at Object.InjectedScript._evaluateOn (<anonymous>:905:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:838:34)
at Object.InjectedScript.evaluate (<anonymous>:694:21)
Try something like:
$.getJSON('sending_msg_to_user.php?userinput=' + InputMessage);