Im just learning javascript the last days (started PHP some months ago).
So, my code make this:
e.g
http://controljuridico.com/video01/
I have three files.
An Html file with the form.
A javascript file with the functions after click on "enviar" (send) button.
A php that process the data.
HTML file (index.html)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</title>
<link href="css/css.css" type="text/css" rel="stylesheet" media="screen" />
<script src="js/jquery.js" type="text/javascript" language="JavaScript"></script>
<script src="js/js.js" type="text/javascript" language="JavaScript"></script>
</head>
<body>
<div id="content">
<h1>Test.</h1>
<h1>Step 1) choose city</h1>
<br />
<br />
<label>test(*)</label>
<hr />
<br />
<form name="form_data" id="form_data">
<label>(*) City</label>
<br />
Medellin
<input type="radio" name="ciudad" value="Medellin" />
Manizales
<input type="radio" name="ciudad" value="Manizales"/>
Cali
<input type="radio" name="ciudad" value="Cali"/>
<br />
<label>(*) Especialidad</label>
<br />
<input type="button" value="Enviar" id="btn_enviar" />
<br />
<br />
<label id="mensaje"></label>
</form>
<div id="resultado" ></div>
</div>
</body>
</html>
js.js File
$(document).ready(function(){
$('#btn_enviar').click(function(){
if( validaRadio( 'ciudad','Ciudad' ) == false) return false;
$.ajax({
type:'POST',
url :'upload.php',
data: $('#form_data').serialize(),
beforeSend : function(){
$('#mensaje').html('Enviando datos...');
},
success: function (data){
$('#mensaje').html('Datos enviados correctamente.');
$('#resultado').html(data);
},
complete: function(){
$('#form_data').slideUp();
$('#resultado').slideDown();
}
});
});
});
Php File (upload.php)
<?php
$sergu = $_POST['ciudad'];
if ($_POST['ciudad'] == "Medellin") {
?>
<label>Ciudad Ingresada:</label>
<br />
<label><?php echo $_POST['ciudad'] ?></label>
<hr />
<?php
}else{
echo "it is not medellin....";
}
?>
So, this works very well but. What if I want this:
After click on "enviar" button also show another similar form at the left of this form.
I mean its just like choosing steps if you choose the step one I need another step and so on and I want that this another step just appear to the left of the previus form.
It is possible? how?
Thanks in advance for your help! I really appreciate it.
You could for example just hide the 'second form' when the page first loads via css like this:
<form id="second_form" style="display: none">...
and when your success function fires you remove the css like so:
success: function(){
...
$('#second_form').show();
}
Short answer: yes.
In jquery, there is .insertBefore(). An easy way to implement what you (kind of) want, is to just echo out the new form in the php, and instead of
$('#resultado').html(data);
Do the following:
$(data).insertBefore('#resultado');
This will insert the new form, echoed out by the PHP, under the previous one. Ofcourse you also have to delete the
$('#form_data').slideUp();
Else, the forms will be hidden.
Related
I have an html page which contains a div that displays the html from an external php file.
It works great until I add a DOCTYPE declaration to the html page. The page continues to function, except the external content does not appear in the div.
<!DOCTYPE HTML>
<html dir="ltr" lang="en-US">
<head>
<title>Test Page</title>
<!--meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"-->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" src="./inc/jquery.js"></script>
<script type="text/javascript">
function getinfo()
{
$.post('prodinfo.php', { prodcode: prodcodeform.prodcodevar.value},
function(output)
{
$('#prodinfo').html(output).show;
});
}
function hideinfo()
{
$('#prodload').hide();
$('#openprodinfo').show();
}
function showinfo()
{
$('#prodload').show();
$('#openprodinfo').hide();
}
</script>
</head>
<body>
<input style="position:relative;" type="button" class="button" id="openprodinfo" title="Open" value="INFO" onclick="showinfo();">
<DIV id="prodload" style="position:absolute;margin-left:auto;margin-right:auto;display:none;text-align:center;background-color:#000000;z-index:200;border:1px solid #4e443b;">
<div id="prodinfo" style="position:relative;display:block;top:0;width:1000px;height:820px;background-color:#ffffff;margin-left:auto;margin-right:auto;">
</div>
<form name="prodcodeform">
<input type="text" readonly="readonly" id="prodcodevar" name="prodcodevar" value="nil" >
</form>
<div ID="prodinfobutton" style="position:relative;">
<input style="position:relative;" type="button" class="button" id="closeprodinfo" title="Close" value="CLOSE" onclick="document.getElementById('prodcodevar').value='nil'; hideinfo(); ">
</div>
<input type="button" id="button001" value="ONE" onclick="document.getElementById('prodcodevar').value='item1'; getinfo();">
<input type="button" id="button002" value="TWO" onclick="document.getElementById('prodcodevar').value='item2'; getinfo();">
</DIV>
</body>
</html>
You are switching to Standards mode, so your browser is no longer playing the game of being compatible with Internet Explorer 4.
prodcodeform.prodcodevar.value will error because prodcodeform is undefined.
You don't get a global variable for every element with an id or name in a document.
Change:
<form name="prodcodeform">
To
<form id="prodcodeform" method="post" action="prodinfo.php">
… and make it do something sane when a non-Ajax request gets posted (move it so it is around the buttons, make them submit buttons, and cancel the default event if the JS succeeds).
Then add:
var prodcodeform = document.getElementById('prodcodeform');
before you try to use the variable.
You started your body with </body> instead of <body>.
This is my first post here and I hope that someone will be able to help me.
For the past week I have been working on a project of mine. Apparently, I have stuck with the last part.
So basically, I have an AJAX chat and when I submit a line I send (using a Post method) the whole line to be analyzed (to a file named analysis.php).
The chat line is being analyzed and find the variable I needed by doing queries on a MySql Database.
All I need now, is to have this variable taken with JQuery-AJAX and put it on a div in my html file(so it can be displayed on the right-left-whatever of the chat).
Here are my files :
analysis.php
<?php
$advert = $row[adverts];
?>
ajax-chat.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX Chat</title>
<link rel="stylesheet" type="text/css" href="js/jScrollPane/jScrollPane.css" />
<link rel="stylesheet" type="text/css" href="css/page.css" />
<link rel="stylesheet" type="text/css" href="css/chat.css" />
</head>
<body>
<div id="chatContainer">
<div id="chatTopBar" class="rounded"></div>
<div id="chatLineHolder"></div>
<div id="chatUsers" class="rounded"></div>
<div id="chatBottomBar" class="rounded">
<div class="tip"></div>
<form id="loginForm" method="post" action="">
<input id="name" name="name" class="rounded" maxlength="16" />
<input id="email" name="email" class="rounded" />
<input type="submit" class="blueButton" value="Login" />
</form>
<form id="submitForm" method="post" action="">
<input id="chatText" name="chatText" class="rounded" maxlength="255" />
<input type="submit" class="blueButton" value="Submit" />
</form>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="js/jScrollPane/jquery.mousewheel.js"></script>
<script src="js/jScrollPane/jScrollPane.min.js"></script>
<script src="js/script.js"></script>
</body>
</html>
So, I am basically trying to get the $advert from the analysis.php file(after the whole analyze is done) , and by using JQuery/AJAX pass it eventually to the ajax-chat.html file.
Any help is really appreciated. I have googled everything but haven't found something to help me.
Thanks in advance.
If I understand right, you need to use JSON. Here is a sample.
In your PHP write:
<?php
// filename: myAjaxFile.php
// some PHP
$advert = array(
'ajax' => 'Hello world!',
'advert' => $row['adverts'],
);
echo json_encode($advert);
?>
Then, if you are using jQuery, just write:
$.ajax({
url : 'myAjaxFile.php',
type : 'POST',
data : data,
dataType : 'json',
success : function (result) {
alert(result['ajax']); // "Hello world!" alerted
console.log(result['advert']) // The value of your php $row['adverts'] will be displayed
},
error : function () {
alert("error");
}
})
And that's all. This is JSON - it's used to send variables, arrays, objects etc between server and user. More info here: http://www.json.org/. :)
In a form I have an <iframe> that contains a PHP file (editor.php). This PHP file contains an HTML form.
Well, when I do a "submit form", I call the same PHP file, for example main.php.
When I press the submit button, I have "onclick method" that it calls a Javascript function inside editor.php. This function executes the form.
My problem is that main form is executed correctly but the second form is not.
In the second loop of the form of editor.php receives nothing.
**Check this way it will work as you expected**
//main.php
<html>
<head>
<script type="text/javascript">
function validateMain()
{
alert('main');
}
function validateSub()
{
alert('sub');
}
</script>
</head>
<body>
<form id="main" onsubmit="return validateMain();">
<input type="text" name="first" id="first"/>
<input type="submit" value="Submit Main"/>
</form>
<iframe name="ifr-form" id="ifr-form" src="test.html"></iframe>
</body>
</html>
//test.html the second form included through iframe
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<form id="sub" onclick="return window.top.validateSub();">
<input type="text" name="first" id="second"/>
<input type="button" value="Submit Sub" />
</form>
</div>
</body>
</html>
you must check if php and iframe are compatible, as far as i Know I don't think that frames and php gives any output, hope this helps.
I'm developing a chatbox script, and I have this page that checks if session is set, and if so, the certain elements of code should be hidden with jQuery. Here are my pages:
input.php:
<!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" />
<style type="text/css">
#import "stil.css";
</style>
<title>Untitled Document</title>
<script type="text/javascript" src="jq.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.13.custom.min.js"></script>
<script type="text/javascript" src="scripts.js"></script>
<script type="text/javascript" src="postme.js"></script>
<?php
include_once('check.php');
?>
</head>
<body>
<div id="wrap">
<div id="chat">
<div id="main">
</div>
<div id="input">
<form name="form"action="test.php" method="post">
<input type="text" name="tekst" id="msg" size="72" />
<input type="submit" name="dugme" value="posalji" id="dugme" />
</form>
</div>
</div>
</div>
<div id="black">
</div>
<div id="name">
<form name="yname">
<input type="text" name="tekst2" />
<input type="button" name="dugme2" value="Enter" onclick='send()' />
</form>
</div>
</body>
</html>
sesion.php:
<?php
session_start();
$_SESSION['ime']=$_POST['ime'];
$sesion_n=$_SESSION['ime'];
echo $sesion_n;
?>
check.php:
<?php
include('sesion.php');
if (!isset($sesion_n)){
echo "<script type='text/javascript'>$('#black').hide();$('#name').hide();</script>";
}
?>
postme.js:
function send(){
$.post('sesion.php',{ime:yname.tekst2.value},function(val){
if(val!=null) {
$('#black').fadeOut();
$('#name').hide();
alert(val);
}
}
)};
So the problem is that I get this error every time I run the page:
Notice: Undefined index: ime in C:\wamp\www\AJAX\sesion.php on line 3.
So can someone tell me what I'm doing wrong here?
if(isset($_POST['ime']))
{
$_SESSION['ime']=$_POST['ime'];
$sesion_n=$_SESSION['ime'];
echo $sesion_n;
}
it seems that $_POST['ime']; is undefined and that means that you are not posting it i guess.
Are you sure that yname.tekst2.value is the correct way to access the value of the field?
If you have firebug you can check in the "console" tab what parametrs have been posted.
It appears you're loading check.php manually. That'd be a GET request, and will trash your stored value, as _POST won't be set on those pages. Probably won't be the cause of the undefined index problem, but something to consider.
Check that the session's ID value stays constant between requests. If it's different each time, you're getting a brand new blank session on each request.
I have a form that I ad an element to via JavaScript before it is submitted.
parent.document.getElementById('submitcomment' + id).innerHTML = '<img class="image_real" src="/images/site.png" alt="Mostly Dirty," />
<input class="real" name="freshness" type="text" size="5" maxlength="6" />
<a href="#" onClick="submitComment('+[id]+'); return false;">
<img id="submitcommentimg<?php echo $id; ?>" src="images/check.png" alt="Comment!" border="0"></a>
<div class="submitcommentalert" id="comment_alert_<?php echo $id; ?>" style="display:none">Comment Posted!</div>';
When the form is submitted it seems to be missing the 'freshness' <input> element
That is if I try to access $_POST['freshness']; it is empty.
There is no value attribute in the freshness, this might be the issue. Have you checked to see of the $_POST['freshness'] even exists (isset())?
When the above code is executed, does the DOM gets updated with the newly added content? Also check with FireBug what's actually being posted to the server. Also you are using parent.document - could there be some iframe issues?
UPDATE:
Trying to reproduce the problem I've come up with this snippet which works as expected (when you submit the form, the value of the freshness input is correctly passed):
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function addDynamicContent() {
document.getElementById('myForm').innerHTML =
'<input name="freshness" type="text" />' +
'Submit';
}
function submitComment() {
document.getElementById('myForm').submit();
return false;
}
</script>
</head>
<body>
<input type="button" value="Add dynamic content" onclick="addDynamicContent();" />
<form id="myForm"></form>
</body>
</html>
Now you could tweak it to your needs.