I have a from page for displaying question which is fetching data from mysql
<?php
include('lib/db.php');
if(isset($_GET['id']))
{
$cid=mysql_real_escape_string($_GET['id']);
//echo $sub;
$rs=mysql_query("select * from category where id='$cid'");
$r=mysql_fetch_array($rs);
$q=rand(1,2);
$rs1=mysql_query("select * from questions where qid='$q'");
$r1=mysql_fetch_array($rs1);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test</title>
<script type="text/javascript" src="js/jquery-2.1.0.js"></script>
<script type="text/javascript" src="js/tsubmit.js"></script>
</head>
<body>
<header>
<?php echo $r['cat_name'];?> Test
</header>
<form method="post" id="test" role="form">
<label for="question">Question:</label>
<?php echo $r1['question']; ?><br/>
<input type="radio" value="<?php echo $r1['ans1']; ?>" name="ans1" /><?php echo $r1['ans1']; ?>
<br/><input type="radio" value="<?php echo $r1['ans2']; ?>" name="ans1" /><?php echo $r1['ans2']; ?>
<br/><input type="radio" value="<?php echo $r1['ans3']; ?>" name="ans1" /><?php echo $r1['ans3']; ?>
<br/><input type="radio" value="<?php echo $r1['ans4']; ?>" name="ans1" /><?php echo $r1['ans4']; ?>
<br/><input type="submit" value="submit" name="submit" id="submit"/>
</form>
</body>
</html>
and the embedded tsubmit.js is
$(document).ready(function()
{
var start;
start= new Date()/1000;
function submit(x){
var end=new Date()/1000;
var timespent=end-x;
alert(timespent);
var ans=$('input[name=ans1]:checked').val();
alert(ans);
var cans="<?php echo $r['ans']; ?>";
//alert('<?php echo $r["ans"]; ?>');
alert(cans);
// var qid="<?php echo $q; ?>";
alert(qid);
//var datastring='ans='+ans+'×pent='+timespent+'cans='+cans+'qid='+qid;
$.ajax({
type:"POST",
url:"result.php",
data:'ans='+ans+'×pent='+timespent+'&cans='+cans+'&qid='+qid //success:success()
});
}
$("#test").submit(function(event){
alert('user clicked submit');
submit(start);
//event.preventDefault();
});
});
but i'm not value of cans and also result.php isn't getting anything
<?php
include('lib/db.php');
echo $_POST['ans'];
echo "<script> alert('ans')</script>";
?>
echo $_post['ans'] isn't getting anything
As #MarcB wrote, the JavaScript files with PHP will not execute. There are ways to do it, yet as was already mentioned, it is not advised.
In your HTML head tag, you cna define a script like so:
<script type="text/javascript" src="js/tsubmit.php"></script>
This will call the file via PHP and result in JS. Still wont get the PHP you're expecting.
Related
I want to generate random no in php per second and want to display that after clicking the generate button. Below is the code for same
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Random no Generator</title>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
</head>
<?php
if(isset($_GET['action'])=='submitfunc') {
submitfunc();
}else
//show form
?>
<body>
<form action="?action=submitfunc" method="post"> <p>
<input type="submit" value="Generate" /></p>
<p>
</form>
</body>
<?php
function submitfunc() {
while(1){
echo rand (5,10)."\n";
sleep(1);
echo rand (25,50)."\n";
sleep(1);
}
}
?>
</html>
Edit:
I want to insert no in database perodicaly so need this to work via php
As said, PHP is a server-side scripting language. This Fiddle may help.
<button id="gen">Generate</button>
<br/>
<div id="content">
</div>
<script type="text/javascript">
var int1 = null;
var int2 = null;
var br = $('<br/>');
$("#gen").on('click', function(){
int1 = setInterval(function(){
var span = $('<span/>').text(~~(Math.random()*5+5));
$("#content").append(span).append($('<br/>'));
},2000);
setTimeout(function(){
int2 = setInterval(function(){
var span = $('<span/>').text(~~(Math.random()*25+25));
$("#content").append(span).append($('<br/>'));
},2000);
},1000);
});
</script>
I want to connect my website with the facebook. So I am trying to make a button that user sign in with there facebook.
What I want to do is: pass users data that I need to create their account from my index.php to test.php (because it is a test after that I will adapt it) ...
problem is I am not able to redirect to test.php ...
I can do that with a form and a button but I want to do it without any button ...
<!-- ---------------------------------------------------------------------------------------
IdiotMinds - http://idiotminds.com
-----------------------------------------------------------------------------------------
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login with Facebook</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/oauthpopup.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#facebook').click(function(e){
$.oauthpopup({
path: 'login.php',
width:600,
height:300,
callback: function(){
window.location.reload();
}
});
e.preventDefault();
});
});
</script>
</head>
<body>
<?php
session_start();
if(!isset($_SESSION['User']) && empty($_SESSION['User'])) { ?>
<img src="images/facebook.png" id="facebook" style="cursor:pointer;float:left;" />
<?php } else { ?>
<form action="test.php" method="post">
<INPUT TYPE = "hidden" name="name" VALUE =<?php echo $_SESSION['User']['first_name'] ?>>
<INPUT TYPE = "hidden" name="lname" VALUE =<?php echo $_SESSION['User']['last_name'] ?>>
<INPUT TYPE = "hidden" name="email" VALUE =<?php echo $_SESSION['User']['email'] ?>>
<INPUT TYPE = "hidden" name="location" VALUE =<?php echo $_SESSION['User']['location']['name'] ?>>
<input type="submit" name="submit"/> /* without this one please */
</form>
<?php
header("Location:test.php?name&lname");
/*
echo '<img src="https://graph.facebook.com/'. $_SESSION['User']['id'] .'/picture" width="30" height="30"/><div>'.$_SESSION['User']['first_name'].'</div><div>'.$_SESSION['User']['last_name'].'</div><div>'.$_SESSION['User']['email'].'</div><div>'.$_SESSION['User']['location']['name'].'</div>';
echo 'Logout';
*/
}
?>
</body>
</html>
There must not be any output before the header() call!
Read the manual here.
I try to submit my form automatically when I change an input field but nothing happens but I can just display a message using "alert(message)",can you help me please!!!
My code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="script/jquery-1.8.3.min.js"></script>
</head>
<body>
<form method="POST" id="form" action="">
<fieldset>
Text 1 : <input id="text1" type="text" name="text1" value="" />
<input id="submit" type="submit" name='submit' value="Send"/>
</fieldset>
</form>
<script>
$('#form input[name=text1]').change(function(){
$('#form').click();
//alert("Changed!");// This works
});
</script>
</body>
</html>
<?php
if (isset($_POST['submit'])){
echo $_POST['text1'];
}
?>
Try this -
$('#form input[name=text1]').change(function(){
$('#form').submit();
});
http://api.jquery.com/submit/
Try:
$('#form input[name=text1]').change(function(){
$("#submit").click();
});
I have never tried to post dynamic textboxes created with javascript to process the data. The only form data that is being sent to the processing page is the submit button value. Any ideas on how to do this so that all textbox data sent through post correctly?
Page Code:
<?php
if(!isset($_GET['saleid'])) {
header('location: dashboard.php');
die();
}else{
include('db_connect.php');
$saleid = mysqli_real_escape_string($mysqli, $_GET['saleid']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script>
$(function() {
$("#date").datepicker({ dateFormat: 'yy-mm-dd' })
$(document).ready(function(){
var counter = 2;
$("#addButton").click(function () {
if(counter>5){
alert("Only 10 textboxes allow");
return false;
}
$("#sale > tbody").append("<tr><td> <label>Date:</label><br /><input type='textbox' id='date" + counter + "' ></td><td> <label>Type:</label><br /><input type='textbox' id='type" + counter + "' ></td><td> <label>Amount:</label><br /><input type='textbox' id='amount" + counter + "' ></td><td><label>Notes:</label><br /><input type='textbox' id='notes" + counter + "' ></td></tr>");
counter++;
});
$("#removeButton").click(function () {
if(counter<3){
alert("No more textbox to remove");
return false;
}
counter--;
$('#sale tr:last').remove();
})
});
});
</script>
</head>
<body>
<?php echo "Sale ID: ". $saleid; ?>
<form id="form1" name="form1" method="post" action="<?php echo "process_saletrans.php?saleid=" . $saleid; ?>">
<h2>Sale Transaction</h2>
<table id="sale" width="300" border="0" cellpadding="2">
<tr>
<td> <label>Date:</label><br />
<input type='textbox' id='date1' ></td>
<td> <label>Type:</label><br />
<input type='textbox' id='type1' ></td>
<td> <label>Amount:</label><br />
<input type='textbox' id='amount1' ></td>
<td> <label>Notes:</label><br />
<input type='textbox' id='notes1' ></td>
</table>
<input type='button' value='Add' id='addButton'>
<input type='button' value='Remove' id='removeButton'>
<p>
<input type="submit" name="submit" id="submit" value="Add Transaction" />
</p>
</form>
</body>
</html>
I believe the issue you're having is that you're using id rather than name. You can use id at the same time, but I'm pretty sure it doesn't set the name of the form field for purposes of submitting the form.
I have the following HTML code:
<form method="post" action="the_file.php" id="the-form">
<input type="text" name="the_input" value="<?php if ( isset( $_POST['the_input'] ) echo $_POST['the_input]; ?>">
<button type="submit" name="eat_something" value="TRUE">Eating</button>
<button type="submit" name="eat_something" value="FALSE">Don't Eat</button>
</form>
<textarea id="result"></textarea>
Followed by this JS:
$('#the-form').bind('submit', submitForm);
function submitForm(evt) {
jQuery.post(
$(this).attr('action'),
$(this).serialize(),
function(data) {
$('#result').empty().append(data).slideDown();
});
evt.preventDefault();
}
I also have a PHP script that receives the $_POST value from the input on submit and runs a conditions to test which submit button was clicked.
Like this:
$input = $_POST['the_input'];
$eating = $_POST['eat_something'];
if ( $eating == 'TRUE' ) {
// Do some eating...
} else {
// Don't you dare...
}
If I don't use the jQuery.post() function the submit values from the button are posted. However, for some reason, I can't manage to pass the button value to PHP $_POST with the jQuery.post() function. If I don't use jQuery.post() the output doesn't get appended to the textarea but rather in a text format on a separate page, like a document. I've also tried calling the submit function on the button $('button[type=submit]').bind('submit', submitForm); but this doesn't solve my problem either.
Thanks in advance for you help.
You forget signle quote and ) in the_name input.
<input type="text" name="the_input" value="<?php if (isset($_POST['the_input'])) { echo $_POST['the_input'] ; } ?>">
and for getting form button pressed value you need to append it value manually to serialize.
$form.serialize() + "&submit="+ $('button').attr("value")
Example
<script type="text/javascript">
$(function()
{
$('button[type="submit"]').on('click',function(e)
{
e.preventDefault();
var submit_value = $(this).val();
jQuery.post
(
$(this).attr('action'),
$(this).serialize()+ "&submit="+ submit_value,
function(data)
{
$('#result').empty().append(data).slideDown();
}
);
});
});
</script>
Complete Tested Code
<?php
if(isset($_POST['the_input']))
{
$input = $_POST['the_input'];
$eating = $_POST['eat_something'];
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>StackOverFlow</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function()
{
$('button[type="submit"]').on('click',function(e)
{
e.preventDefault();
var submit_value = $(this).val();
jQuery.post
(
$('#the-form').attr('action'),
$('#the-form').serialize()+ "&eat_something="+ submit_value,
function(data)
{
$('#result').empty().append(data).slideDown();
}
);
});
});
</script>
</head>
<body>
<form method="post" action="random.php" id="the-form">
<input type="text" name="the_input" value="<?php if (isset($_POST['the_input'])) { echo $_POST['the_input'] ; } ?>">
<button type="submit" name="eat_something" value="TRUE">Eating</button>
<button type="submit" name="eat_something" value="FALSE">Don't Eat</button>
</form>
<textarea id="result"></textarea>
</body>
</html>
Simplest answer would be:
<form method="post" action="the_file.php" id="the-form">
<input type="text" name="the_input" value="<?php if ( isset( $_POST['the_input'] ) echo $_POST['the_input']; ?>">
<input type="submit" name="eat_something" value="Eating" />
<input type="submit" name="eat_something" value="Don't Eat" />
</form>
Of course, this won't give exactly what you're looking for.
You can also do something like this:
<form method="post" action="the_file.php" id="the-form">
<input id="theInput" type="text" name="the_input" value="<?php if ( isset( $_POST['the_input'] ) echo $_POST['the_input']; ?>">
<button type="button" name="eat_something" data-value="TRUE">Eating</button>
<button type="button" name="eat_something" data-value="FALSE">Don't Eat</button>
</form>
$('#the-form button').bind('click', function(){
jQuery.post($('#the-form').attr('action'),
{
the_input: $('#theInput').val(),
eat_something: $(this).attr('data-value')
},
function(data) { $('#result').empty().append(data).slideDown() },
'json'
});
Change the buttons to this (change w/your form names)..
<input type="hidden" name="eat_something" value="" />
<input type="button" onclick="$('input[name=eat_something]').val('TRUE')" />
<input type="button" onclick="$('input[name=eat_something]').val('FALSE')" />
The Javascript function and the PHP are fine.
Thanks!
#leo