actually i'm new to web and php,
i'm building a website,
i would like to introduce chat session there but, when the user click the insert button that's not working (failing to pass the paramerts to another page)
Help me out...!!!
here is the code
chat_page.php
<input type="text" id="txtmsg" name="txtmsg" size="25"> <input type="button" value="send" onclick="sndmsg()">
jscript.js
function sndmsg() {
msg=document.getElementById('txtmsg').value;
$.post("insert.php",{u:un,m:msg},function(){ });
document.getElementById('txtmsg').value="";
}
insert.php
include("connec.php");
$chatmsg=mysql_real_escape_string($_REQUEST['m']);
$uname=mysql_real_escape_string($_REQUEST['u']);
echo $chatmsg;
echo $uname;
mysql_query("INSERT INTO `mdi_chat_msg`(`uname`, `chatmsg` ) VALUES ( '$uname','$chatmsg')") or die(mysql_error());
What is un in jscript.js. Maybe, that parameter is unknown.
$.post("insert.php",{u:un,m:msg},function(){ });
You should declare un and msg variables;
var un, msg;
Check you have declare and change the type="submit" instead type="button" you cant use the word msg as variable and cant pass the js value in ajax and check un variable.
<form>
<input type="text" id="txtmsg" name="txtmsg" size="25"> <input type="submit" value="send" onclick="sndmsg()">
</form>
<script>
function sndmsg() {
msg1=$("#txtmsg").val();
$.post("insert.php",{u:un,m:msg},function(){ });
document.getElementById('txtmsg').value="";
}
$.ajax({
type: "POST",
url: "insert.php",
data: { u: un,m:msg1},
});
</script>
Related
Good evening SO community,
I'm trying to build a global chat system for my network of websites. In other words, a staff member can log in to www.myadminswebsite.com and check the live chat systems from all of our other external sites. I have the system working fairly well, except for the fact that the page is refreshing every time a user submits a new message. Is there something I can do to avoid refreshing the page to submit a message? Currently, I'm using an HTML form which posts to itself, then the page checks to see if the $_POST["var"] exists, then writes to the IM log file.
Code From HTML Form
<form method='POST' action='" . $_SERVER['PHP_SELF'] . "'>
<input type='text' name='newMSG' id='lcTextInput' placeholder='Type a Message'>
<input type='submit' value='Send'>
</form>
Function to Process POST
if (isset($_POST['newMSG'])) {
$wHandle = fopen($lFile, "a");
fwrite($wHandle, "[CUSTOMER] " . $_POST['newMSG'] . "\n");
fclose($wHandle);
}
This does what I need it to do, other than refreshing the page. Please let me know if you need any more information or if you have any ideas!
Thanks in advance,
Tim
<form>
<input type='text' name='newMSG' id='lcTextInput' placeholder='Type a Message'>
<input type='submit' value='Send' id="submit">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$('#submit').click(function()
{
var message=$("#lcTextInput").val();
$.ajax({
url: "msg.php",
type:'POST',
data:
{
action: 'addmsg',
message: message
},
success: function(msg)
{
$(".li").append(message);
}
});
return false;
});
</script>
Change your form action to:
<form method='POST' action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type='text' name='newMSG' id='lcTextInput' placeholder='Type a Message'>
<input type='submit' value='Send'>
</form>
$('#submit').click(function()
{
var message=$("#lcTextInput").val();
$.ajax({
url: "msg.php",
type:'POST',
data:
{
action: 'addmsg',
message: message
},
success: function(msg)
{
$(".li").append(message);
}
});
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type='text' name='newMSG' id='lcTextInput' placeholder='Type a Message'>
<input type='submit' value='Send' id="submit">
</form>
I have form, which when is submitted, is making ajax POST request, posting data fetched from form. Form is quite straight forwards.
<form id="form1">
<input type="text" name="domain" id="field1">
<input type="submit" name="my-submit" id="my-submit" value="Submit Form" class="btn btn-info btn-lg">
<!--<button type="button" class="btn btn-info btn-lg" id="myBtn">Open Modal</button>-->
</form>
Then #response div where returned content is loaded.
# HTML code ...
<div class="modal-body" id="response">
<p>Some text in the modal.</p>
</div>
# HTML code ...
And finally JavaScript...
$('#form1').submit(function(event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: 'check.php',
data: $(this).serialize(),
success: function (data) {
//console.log(data);
$('#response').html(data);
$("#myModal").modal();
}
});
});
But then, I want to check in my index.php script, where all these above is, if is $domain variable passed in URL, like yoursite.com?domain=domain.com, and if is, to call submit(); function programatically with this code:
if(isset($_GET['domain'])) {
$domain = $_GET['domain'];
echo $domain;
echo "
<script>
$('#form1').submit();
</script>
";
//exit();
}
Problem is, that nothing happens. How to achieve desired behaviour?
You should not do that. There is no reason to serve a page to the client, fill a form automatically and post back to the server.
Instead you should check if $_GET['domain'] domain is set and if it is, you should include your check.php file directly. And you should adapt check.php to handle both POST and GET parameters.
This would save you a round-trip to the client and does not rely on the client having javascript enabled.
<form id="form1">
<input type="text" name="domain" id="field1">
<input type="submit" onclick="return call();" value="Submit Form">
</form>
<script type='text/javascript'>
function call()
{
//ajax code;
return true;//are your want form submit
return false;// are your want cannot form submit
}
</script>
Don't forget to put return
<script>
$(document).ready(function(){
$("#date").click(function(){
start = $("#date_start").val();
end = $("#date_end").val();
});
});
</script>
<input name='date_A' type='text' id='date_start' />
<input name='date_B' type='text' id='date_end' />
<input type="button" id="date" class="button" value="check" />
The question is how to get value of "start" and "end" from the jquery to put that in PHP variable without post it first to database?
<?php
$start_date = "";
$end_date = "";
?>
The both variables above need the value of the variable "start" and "end" from jquery above
$(document).ready(function(){
$("#date").click(function(){
start = $("#date_start").val();
end = $("#date_end").val();
$.ajax({
type: 'post',
url: '/your_page_link/',
data: {start : start, end : end},
success: function (res) {
}
});
});
});
In PHP:
<?php
if(!empty($_POST)){ // If you are using same page, then it'll help you to detect ajax request.
$start_date = $_POST['start'];
$end_date = $_POST['end'];
}
?>
I fear you want to read values from jQuery and inject them in PHP in the same page, which is impossible.
You have to understand what PHP is first. It generates the page, that is then sent to the browser, that then executes jQuery code. PHP code completely disappeared from the generated page. jQuery has no idea of PHP's presence, they just can't interact this way. You must use a GET or POST query in order to send values to a PHP page, back to the server.
You want to read things like this : Difference between Javascript and PHP
This is what you have to do.
<form id="form" name="form" action="" method="post">
<div id="block">
<input type="text" name="startDate" id="startDate" placeholder="Start date" required/>
<input type="text" name="endDate" id="endDate" placeholder="End date" required />
<input type="submit" id="Download" name="Download" value="Download"/>
</div>
</form>
and put this code in the same page
<?php
$startDate = $_POST['startDate'];
$endDate = $_POST['endDate'];
?>
I'm having a brain freeze at the moment..
I have a variable here in this bit of JavaScript code...
<script type="text/javascript">
$('#menuNameEdit').val(reservationId);
</script>
I have managed to use #menuNameEdit in form input as below but I don't know how to use it as a normal variable in PHP. For example if I want to compare it's value against another variable. Mind Block!
<input id="menuNameEdit" name="reservationId" type="text"
class="form-control" disabled>
You can't. It's just not possible. If JS starts with its work, PHP is already done.
The only way to do that - though its extremely vulnerable - is via AJAX:
$.ajax({
type: "POST",
url: "your_php_script.php",
data: {var: your_variable},
success: function() {
console.log("ajax successful!");
}
});
Without jQuery:
var request = new XMLHttpRequest();
request.open('POST', 'your_php_script.php', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(your_variable);
This needs to be in your_php_script.php:
$jsVariable = $_POST['var'];
This is extremely vulnerable though - always sanitize input via filter_var() or htmlspecialchars() which could be modified by the user!
<?php
if (isset($_POST['submit'])) {
$reservationId = $_POST['reservationId'];
$reservationId2= $_POST['reservationId2'];
//comparison
}
?>
<form class='addcategory' method="post" enctype="application/x-www-form-urlencoded">
<input id="menuNameEdit" name="reservationId" type="text"
class="form-control" disabled>
<input id="another" name="reservationId2" type="text"
class="form-control" >
<input name="submit" type="submit" value="submit">
</form>
If your form gets submitted you can access the below code:
PHP
if(isset($_POST['reservationId'])){
echo $_POST['reservationId'];
}
HTML
<form method="" action="" id="form-id">
<input id="menuNameEdit" name="reservationId" type="text" class="form-control" disabled>
<input name="submit" type="submit" class="form-control">
</form>
Jquery
var menuNameEdit = $('#menuNameEdit').val();
$.ajax({
type: "POST",
url: "index.php",
data: {menuNameEdit : menuNameEdit },
success: function() {
console.log("Success!");
}
});
index.php
echo $_POST['menuNameEdit'];
I have a form on page 1 and I want to parse its variables to ajax call on page 2. The ajax call is triggered by on onload event.
Scenario:
Page1
<form id="form1"method="GET" action="page2">//send the variables to page 2
<input type="text" name="Place" value="city">
<input type="text" name="Type" value="room">
<input type="submit"></form>
page 2
<form name="myform2" id="myform2" method="GET">
<input type="text" name="Place" value="<?php echo $_GET[Place] ?>">//
<input type="text" name="type" value="<?php echo $_GET[Type] ?>">
<button id="submit2"type="submit" value="submit2" name="submit2" onclick="return ss()">
js1
$(document).ready(function(){ // load file.php on document ready
function sssssss2(page){
var form2 = document.myform2;
var dataString1 = $(form2).serialize() + '&page=' + page;
({
type: "GET",
url: "file.php",//
data: dataString1,
success: function(ccc){
$("#search_results").html(ccc);
}});}
sssssss2(1) ;
$('#search_results .pagination li.active').live('click',function(){
var page = $(this).attr('p');
sssssss2(page);
});
});
js2
function sss() {//serialize the form each time submitted.
var form2 = document.myform2;
var dataString1 = $(form2).serialize();
$.ajax({
type:'GET',
url: "file.php",
cache: false,
data: dataString1,
success: function(data){
$('#search_results').html(data);
}
});
return false;
}
The problem is the file.php doens't take the variable "city" and "room".I would like to parse the 2 variable to file.php when page2 load first time.
Hw to parse those variable on document load page2?
Shouldn't Place and Type be in quotes like
<input type="text" name="Place" value="<?php echo $_GET['Place'] ?>">//
<input type="text" name="type" value="<?php echo $_GET['Type'] ?>">
Here you miss a blankspace before 'method' and before 'type' attribute:
<form id="form1"method="GET" action="page2">
<button id="submit2"type="submit" value="submit2" name="submit2" onclick="return ss()">
Here you miss an action attribute:
<form name="myform2" id="myform2" method="GET">
This is wrong² because you miss a ; and you put a constant instead of a string in the index:
<?php echo $_GET[Type] ?>
correct:
<?php echo $_GET['Type']; ?>
In js1 you miss $.ajax({ + on line 4 ({ is not correct either.
Learn about valid HTML, proper PHP and some more basics I suggest, it can't work this way, especially not cross-platform compatible.
You can't copy scripts together from 100 tutorials and hope they will work, you have to know each command and line of code and what it does.