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.
Related
I am getting image data in base64 format from hidden textarea and that value
is getting saved through ajax and image is beign generated for the first time i
submit form using ajax, in the ajax success method i am getting new image url
that i show using $('.contained').attr("src",data); but for the second time
when i upload the image i did not get the value in the textarea so no image
is beign generated, but as i refresh my page and then submit again then it
is working properly i don't want to refresh the page. Please help to sort out my issue.
View:
<script type="text/javascript">
$("#upload_course").on('submit',function(e){
e.preventDefault();
var txtareaval = $('#image_source').val();
var baseurl="<?php echo base_url(); ?>";
var formData = new FormData($("#upload_course")[0]);
$.ajax({
url: "<?php echo base_url();?>upload_course_image/do_upload",
type: 'POST',
async : false,
cache : false,
contentType : false,
processData:false,
data: formData,
success: function(data){
$('.loader').show();
$('.contained').attr("src",data);
setTimeout(function () {
$('.loader').hide();
$("#show_image_preview").show();
}, 1000);
},
error: function(){
alert('Error while request..');
}
});
});
</script>
<form enctype="multipart/form-data" method="post" accept-charset="utf-8" id="upload_course" action="<?php echo site_url('upload_course_image/do_upload'); ?>">
<input type="hidden" name="c_id" value="<?=$result;?>" />
<img class="contained" data-purpose="image-preview" style="max-width:500px;width:500px;height: 300px;" src="<?php echo $image; ?>" />
<textarea id="image_source" name="test" style="display:none"></textarea>
<input type="File" name="course_img" id="course_img"/>
<input type="submit" name="submit" value="Save" class="btn btn-primary btn btn-success" id="submit-id-submit"/>
</form>
Add the current time to end of your image like this
$('.contained').attr("src",data + '?' + new Date().getTime()).show());
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>
<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 want voting form (in index.php) to submit without the page reload and get results from external page in index.php
HTML
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
</head>
<div class="polling" id="polling_id">
<br><br>
<form id="poll_form" method="POST" action="process-vote.php" />
<div class="poll_objects">
<input type="hidden" name="o1" value="<?php echo $option1; ?>" />
<input type="hidden" name="o2" value="<?php echo $option2; ?>" />
<input type="hidden" name="o3" value="<?php echo $option3; ?>" />
<span class="footer_font"><input type="radio" name="radio_1" value="<?php echo $option1;?>" id="radio" /><label for="radio"><?php echo $option1;?></label> </span><br><br>
<span class="footer_font"><input type="radio" name="radio_1" value="<?php echo $option2;?>" id="radio2" /><label for="radio2"><?php echo $option2;?></label></span><br><br>
<span class="footer_font"><input type="radio" name="radio_1" value="<?php echo $option3;?>" id="radio3" /><label for="radio3"><?php echo $option3;?></label></span><br><br>
<div class="float_buttons">
<input type="submit" name="submit_vote" value="Vote!" class="button" />
<input type="submit" name="results" value="Poll Results" class="button"/>
</div>
</div>
</div>
</form>
Ajax
<script>
$(document).ready(function() {
$.ajax({
$('#poll_form').submit(function(e) {
e.preventDefault();
// note where the e comes from.
var a = $(this).serialize();
$("#polling_id").load("poll-results.php");
});
});
});
</script>
Looking at this coding, for me it seems everything is correct. However the page opens the action page of the form. Please provide help.
First of all your code does not compile, because you are not doing an ajax call, you are passing a form submit to an object literal which will cause an error after the execution of form submit, that why your form is reloading.
<script>
$(document).ready(function() {
$.ajax({ //this is object literal notation and you are returning a function value to it... doesn't make any sense.
$('#poll_form').submit(function(e) {
e.preventDefault();
// note where the e comes from.
var a = $(this).serialize();
$("#polling_id").load("poll-results.php");
});
});
});
</script>
Also an ajax call should look like this:
$.ajax({
url: "test.html",
cache: false
})
.done(function( html ) {
$( "#results" ).append( html );
});
See this link it's all here.
Also if you are trying to perform a load of data based on the jquery.load you shouldn't perform a submit, but you can send data within your load request:
$( "#feeds" ).load( "feeds.php", { limit: 25 }, function() {
alert( "The last 25 entries in the feed have been loaded" );
});
Here you are passing parameter limit, but you can be passing name, address, age, etc.
See load documentation here, there is no need for you to perform a submit.
Regards.
Just remove the ajax call and try if it works
$(document).ready(function() {
$('#poll_form').submit(function(e) {
e.preventDefault();
// note where the e comes from.
var a = $(this).serialize();
$("#polling_id").load("poll-results.php");
});
}