I have been looking for a way to add the information (string) from a variable in the previous page.
As far as I know this should be possible using javascript somehow.
New one couldn't get the old one to work properly..
<script type="text/javascript">
function newPopup(url) {
popupWindow = window.open(
url,'popUpWindow','height=510,width=350,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
</script>
Armory Avatar
I have that piece of code that opens the link into a new popup window(which remembers the url of the previous page).
In this window people can insert some information about there WoW character and the realm this character is on. After they do this and hit submit the site displays the url for the avatar retrieved from the blizzard armory.
http://eu.battle.net/static-render/eu/(imagepath)
Code for the popup page: Updated this current code (7-4-2012)
<?php
if (!isset($_POST['submit'])) {
?>
<!-- The fill in form -->
<html>
<head>
<title>Armory Avatar</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Character Name:<input type="text" size="12" maxlength="50" name="cname"><br />
Realm Name:
<select name="rname">
<optgroup label="English Realms">
<option value="aerie-peak">Aerie-Peak</option>
<option value="agamaggan">Agamaggan</option>
<option value="aggramar">Aggramar</option>
etc etc etc.
</optgroup>
</select><br />
<input type="submit" value="submit" name="submit">
</form>
<?php
} else { //If form is submitted execute this
$charname = $_POST["cname"]; //input character name
$realmname = $_POST["rname"]; //input realm name
$charurl = urlencode(utf8_encode($charname)); //prepares character name for url usage
$realmurl = 'http://eu.battle.net/api/wow/character/'.$realmname.'/'; //combines the basic url link with the realm name
$toon = $realmurl.$charurl; //adds the charactername behind the realm url
$data = #file_get_contents($toon); //retrieves the data from the armory api
if ($data) {
// if armory data is found execute this
$obj = json_decode($data); //converts the data from json to be used in the php code ?>
<img src='http://eu.battle.net/static-render/eu/<?php echo $obj->thumbnail; ?>'> </img><br /> <?php //Is url link to image
$armoryname = utf8_decode($obj->name); //makes the name readable again
echo "Name: " . $armoryname . "<br />"; //character name
echo "Level: " . $obj->level . "<br />"; //character level
echo "Achievement Points : " . $obj->achievementPoints . "<br />"; //Achievement Points
if ( $obj->gender == 1 ){ //Deteremines the gender of the character
echo "Gender : Female <br />" ; //displays gender as female
}else{
echo "Gender : Male <br />" ; //dispalays gender as male
}
$image = "http://eu.battle.net/static-render/eu/".$obj->thumbnail;
?>
Image: <a href='http://eu.battle.net/static-render/eu/<?php echo $obj->thumbnail; ?>'> http://eu.battle.net/static-render/eu/<?php echo $obj->thumbnail; ?></a><br />
<!--Button submit code-->
<script type="text/javascript">
$('button.cancel_link').click(function() {
// Change the value of the input with an ID of 'avatarurl'
// with the dynamic value given to you by the external JSON link
window.opener.getElementById('avatarurl').value = '<?php echo $image; ?>';
});
</script>
<input> <!-- The button here -->
<?php
}
else { // if armory data is not found execute this ?>
error code stuf
}
}
?>
Now i need this line of code:
$image = "http://eu.battle.net/static-render/eu/".$obj->thumbnail;
To be returned when the window is closed or simply by hitting another submit button(prefered to happen on close over button). And when either of those happen it needs to insert this into this string:
<input type="text" class="textbox" name="avatarurl" size="25" maxlength="100" value="{$avatarurl}" /></td>
The texbox called avatarurl.
Hopefully any of you know how to modify or create a javascript that does this for you. Since my php is already severely limited and my javascript knowledge is next to none.
You need to modify the way you're closing your pop-up window. Try something like this:
// When a BUTTON with the class name 'cancel_link'
// is clicked, it activates the following
$('button.cancel_link').click(function() {
// Change the value of the input with an ID of 'avatarurl'
// with the dynamic value given to you by the external JSON link
window.opener.getElementById('avatarurl').value = '<?php echo $image; ?>';
});
You need to make sure your closing link has cancel_link as its class name, and that your input element in your parent document has an id of avatarurl.
So after searching trying and thanks to #Jamie i knew where to look.
I found http://www.codingforums.com/showthread.php?t=213298
And this was finally the thing that worked.
On the php page to open it i added:
<script type="text/javascript">
function open_pop(){
window.open('armory.php','AvatarArmory','height=510,width=350,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
</script>
<html>
.....
<input type="button" value = "OpenArmory" onClick="open_pop()" />
And added id="blogbox" to the input for the textbox.
<input type="text" class="textbox" name="avatarurl" size="45" value="{$avatarurl}" id="blogbox"/>
On the armory.php page i added this button with the javascrip function:
<script type="text/javascript">
function pops(avatar){
textcontent=opener.document.getElementById("blogbox").value;
opener.document.getElementById("blogbox").value = textcontent + " " + avatar;
}
</script>
<input type="button" value = "Insert Avatar.." onClick="pops('<?php echo $image; ?>');window.close()" />
And that worked perfectly.
Thank you jamie for the help.
Related
I have a submit button in my form which when clicked, the text in my text field disappears. I basically need the text to stay because I have another button that requires the text in that text field. The two buttons are parts of two different functions and I was wondering if there was a way to use the value of the variable in the function of one button in the function of the other.
For example, here is the code for one of the buttons:
Enter customer name<input type="text" name="cname">
<input type="submit" name="sub" value="Display">
<?php
if(isset($_GET['sub'])){
display();
}
function display(){
include 'config.php';
$searchstr=$_GET['cname'];
echo "<input type='hidden' name='cname1' value=".$searchstr.">";
$td=$_GET['cname1'];
$sql="select * from info where cname='$searchstr';";
$result=mysqli_query($conn, $sql);
if($searchstr==""){
$message="Please enter a customer name";
echo "<script type='text/javascript'>alert('$message');
window.location.href='retreive.php';</script>";
}
else{
echo "<table id='info' border='1px solid black'><tr padding='2'><th>Customer Name</th><th>Purchase Order Date</th><th>Started?</th><th>Reason (If any)</th><th>Tentative start date</th><th>Progress</th><th>Current Status</th><th></tr>";
while ($row = $result->fetch_assoc()) {
$cname=$row['cname'];
$podate=$row['podate'];
$started=$row['started'];
$reason=$row['reason'];
$tentdate=$row['tentdate'];
$progress=$row['progress'];
$status=$row['status'];
echo "<tr><td>".$cname."</td><td>".$podate."</td><td>".$started."</td><td>".$reason."</td><td>".$tentdate."</td><td>".$progress."</td><td>".$status."</td></tr>";
}
echo "</table><br>";
}
Everything works perfectly here and displays the table as required. But notice the variable $td above. I need that value displayed when my other button is clicked, which is in a different function.
Here's the code for the other button:
echo "<input type='submit' name='fp' value='Finished payment'>";
if(isset($_GET['fp'])){
echo $td;
}
Clicking that button doesn't display anything which means I am not able to read this variable outside the display function.I have tried looking up global variables in php and another solution was to use the and then use Javascript but I want to use php and I need the text to remain in the text field after the submit button is clicked so that I can read it later.
Thank you.
Once you submit the form, the text entered by the user disappeared? It's the default behavior of the browser. To avoid this either submit form using jQuery or store the value of the input into variable and echo.
Sample code for Jquery:
$("#myform").submit(function(e) {
//prevent Default functionality
e.preventDefault();
//get the action-url of the form
var actionurl = e.currentTarget.action;
//do your own request an handle the results
$.ajax({
url: actionurl,
type: 'post',
dataType: 'application/json',
data: $("#myform").serialize(),
success: function(data) {
... do something with the data...
}
});
});
==If not using Jquery==
PHP:
$myValue = $_POST['my_input_name'];
HTML:
<input type="submit" name="sub" value="<?= $myValue ?>">
You can simply create a variable for the value, then place that variable in the value section of the other input.
$name= 'Enter Name';
if(isset($_GET['cname'])){
$name = $_GET['cname'];
}
Then
<input type="text" name="cname" value="<?=$name?>">
Using placeholder attribute:
<input type="text" placeholder="Enter Name" name="cname" value="<?php echo $name; ?>
Before the $_GET['cname'] has been processed, the value within the input field is set to the text you wish to show, or simply keep it empty. Then once the $_GET['cname'] has been posted, you check to see if it is set, then set the variable to equal the returned information the user entered in the other field - $td.
<?php
$cname = ''; // or $cname = 'Enter a Name';
if(isset($_GET['sub'])){
display();
}
function display(){
include 'config.php';
$searchstr=$_GET['cname'];
$td=$_GET['cname'];
$sql="select * from info where cname='$searchstr';";
$result=mysqli_query($conn, $sql);
if($searchstr==""){
$message="Please enter a customer name";
echo "<script type='text/javascript'>alert('$message');
window.location.href='retreive.php';</script>";
}
else{
//use isset to see if that has been posted
if(isset($_GET['cname'])){
$name = $_GET['cname'];
//$name is now set to the value you wish to display in the value of the other input
//call on the value like so <input type="text" name="cname" value="<?=$name?>">
}
echo "<table id='info' border='1px solid black'><tr padding='2'><th>Customer Name</th><th>Purchase Order Date</th><th>Started?</th><th>Reason (If any)</th><th>Tentative start date</th><th>Progress</th><th>Current Status</th><th></tr>";
while ($row = $result->fetch_assoc()) {
$cname=$row['cname'];
$podate=$row['podate'];
$started=$row['started'];
$reason=$row['reason'];
$tentdate=$row['tentdate'];
$progress=$row['progress'];
$status=$row['status'];
echo "<tr><td>".$cname."</td><td>".$podate."</td><td>".$started."</td><td>".$reason."</td><td>".$tentdate."</td><td>".$progress."</td><td>".$status."</td></tr>";
}
echo "</table><br>";
}
php fiddle: Good for 48 hours only - http://main.xfiddle.com/8f416cc9/input.php
I need to get values from a postgre db and use the fopen to open the link inside its records to open real xml file.
<?php
echo "<form id=read2 method=post action=read2.php>";
//other html and table codes
while ($row = pg_fetch_row($result)) {
echo "<tr><td>$row[1]</td><td><input type=hidden name=data value=$row[3] /><a href=javascript:; onclick=document.getElementById('read2').submit();>$row[2]</a></td><td>$row[4]</td><td>$row[5]</td></tr>";
}
the read2.php:
<?php
$data=$_POST['data'];
$explode = explode("/inbox/", $data);
$final = "";
$final.="data/";
$final.=$explode[1];
echo "Result of data before explode is: $data <br />";
echo "Result of data after explode is: $explode[1] <br /><br />";
$myfile = fopen("$final", "r") or die("<h1>Unable to open file!</h1>");
$xml = htmlspecialchars(fread($myfile,filesize("$final")));
?>
<pre>
<?php echo $xml; ?>
</pre>
<?php fclose($myfile); ?>
My problem is here: <input type=hidden name=data value=$row[3] />
I am able to pass to read2.php the correct value and use the explode to adjust what I really need, but I am not able to chose which value to get due to name=data which is the same for all and the read2.php only get the last one in list.
I tried with a counter inside the while: name=data[count]; count++
But in this case I do not know how to get "name" from $_POST
It is also possible that the javascript code I use to send the form is not the best for this situation. Can you please help me fix?
I think I understand what the problem is but please excuse me if I'm off target with what follows. If you were to approach this slightly differently it should be relatively easy.
Have one form separate from the table that contains the recordset data and links - the form need only have the one hidden element which you target using javscript and set the value dynamically.
The following is not tested so it might not work "out of the box" but I think the idea is sound.
<!doctype html>
<html>
<head>
<title>Table data - form submission</title>
<script type='text/javascript'>
function submitdata(event){
var el=typeof( event.target )!='undefined' ? event.target : event.srcElement;
var data=document.getElementById('data');
data.value=el.dataset.value;
data.parentNode.submit();
}
</script>
</head>
<body>
<?php
echo "
<!--
this is the form that actually sends data.
Programatically set the value of the hidden element
and submit the form
-->
<form name='senddata' method='post' action='read2.php'>
<input type='hidden' id='data' name='data'/>
</form>
<table>";
while( $row = pg_fetch_row( $result ) ) {
echo "
<tr>
<td>{$row[1]}</td>
<td>
<a href='#' data-value='{$row[3]}' onclick='submitdata(event)'>{$row[2]}</a>
</td>
<td>{$row[4]}</td>
<td>{$row[5]}</td>
</tr>";
}
echo "
</table>";
?>
</body>
</html>
You should move the hidden parameter from multiple to single and set the hidden parameter value using jquery or javascript and submit the form. Try as below :
<?php
echo "<form id='read2' method='post' action='read2.php'>";
echo "<input type='hidden' name='data' id='data'/>";
echo '<table>';
while ($row = pg_fetch_row($result)) {
echo "<tr><td>$row[1]</td><td><a dataval=$row[2] href='#' onclick='callfunc(this)'>$row[2]</a></td><td>$row[4]</td><td>$row[5]</td></tr>";
}
echo '</table>';
echo '</form>';
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
function callfunc(obj)
{
var dataval = $(obj).attr('dataval');
$('#data').val(dataval);
$('#read2').submit();
}
</script>
Above code is tested and working.
i have a database Included 3 columns " name " , " barcode " , " price " and have a form like blow :
http://i.stack.imgur.com/MwHIw.jpg
i want when user fill the barcode input and press " Enter " key
do form like below pic :
http://i.stack.imgur.com/KPuXv.jpg
and the input names have be like blow :
<HTML>
<form action="do.php" method="Post">
name : <input name="name[]" type="text" />
barcode: <input name="barcode[]" type="text" />
price : <input name="price[]" type="text" />
</form>
</HTML>
--- Update 1 ---
Jquery Source
<script type="text/javascript">
$(document).ready(function(){
$("#text").keyup(function(){
var key = $("#text").val();
var active = true;
$.post("search.php",{key:key,active:active},function(data){
$("#result").html(data);
});
});
});
</script>
<?php
include "config/connect.php";
if(isset($_post["active"]) && $_post["key"])
{
$key = $_post["key"];
$sql = "SELECT * FROM `product_list` WHERE `barcode` LIKE :title ";
$result = $connect->prepare($sql);
$k = "%".$key."%";
$result->bindParam(":title",$k); // ----> i have a error here ! <----
if($result->execute())
{
if($result->rowCount()>0)
{
while($rows=$result->fetch(PDO::FETCH_ASSOC))
{
echo $rows["name"]."<hr />";
echo $rows["barcode"]."<hr />";
echo $rows["price"]."<hr />";
}
}
else
{
echo "i can't fount anything . please try another barcode";
}
}
}
?>
How echo result in a new inputs ?!
how Fix error ?!
what is problem ?!
Add more information here. Show what you have already written in PHP.. You tagged php and such, but what you are asking for is lots of basic info on the web..
Show us how far you are with coding, then we can help. Without you doing anything except html code, people here aren't going to help u.. no offense.
I managed to get my API lyrics code working. All I'm facing is a small problem: When a user enters a song name in a textbox and clicks the submit button, I catch the value via getElementById, and then how do I append it with the URL below?
Here's my code:
<?php
//Catches the value of the Submit button:
$submit = isset($_POST['submit']);
if($submit) {
?>
<script type="text/javascript">
var val1 = document.getElementById('val').value;
</script>
<?php
/* The below $res contains the URL where I wanna append the caught value.
Eg: http://webservices.lyrdb.com/lookup.php?q=Nothing Else Matters(Or
what the user searches for)&for=trackname&agent=agent
*/
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q='+val1+' &for=trackname&agent=agent");
?>
<html>
<form method="post" action="">
Enter value: <input type="text" name="value" id="val" /><br/>
<input type="submit" value="Submit" name="submit" />
</form>
</html>
Could you please correct me as to where I'm making a mistake in this piece of code, highly appreciate all help in this forum! :)
As far as I understand your question, what you need to do is:
<?php
//Catches the value of the Submit button:
$submit = isset($_POST['submit']);
if($submit) {
$val1 = $_POST['val']; // TO store form passed value in "PHP" variable.
/* The below $res contains the URL where I wanna append the caught value.
Eg: http://webservices.lyrdb.com/lookup.php?q=Nothing Else Matters(Or
what the user searches for)&for=trackname&agent=agent
*/
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=' . urlencode($val1) . ' &for=trackname&agent=agent");
} // This is to end the "if" statement
?>
and then change the form input field to:
Enter value: <input type="text" name="val" id="val" /><br/>
I am not sure if POST accepts id values too!
Remove the javascript it's unnecessary. PHP runs at the server. Javascript at the client.
Then change your PHP code to this:
if(isset($_POST['value'])) {
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=". $_POST['value'] ." &for=trackname&agent=agent");
}
I'm a beginner to PHP and I have a quick question. Is it possible to have javscript code injected into a document through php?
For example, suppose I wanted to inject a button that called a function after a form is filled out:
<html>
<head>
<script type = text/javascript>
function func() {
...
}
</script>
</head>
<body>
<form action = 'welcome.php' method = 'post>
Name: <input type = 'text' name = 'fname' />
<input type = 'submit' value = 'submit' />
</form>
<?php
if (isset($_POST['fname'])) {
echo '<input type = button onclick = func />'
}
?>
</body>
</html>
Would this work? If not, why not?
Any help would be great
Will work fine - just remember to close your quoted strings :
<form action='welcome.php' method='post'> // add ' here
Name: <input type='text' name='fname' />
<input type='submit' value='submit' /> // removed a couple of spaces (not required)
</form>
<?php
if (isset($_POST['fname'])) {
echo '<input type="button" onclick="func" />'; // added some quotes and a semicolon
}
?>
I have removed the spaces between the attributes and the values in HTML from type = "something" to type="something" although I can't find the "official" specification on this. It seems that the spaces are perfectly valid ...I think its my personal preference to have no white space there .....
Try:
echo '<input type="button" onclick="' . $_POST['fname'] . '" />';
SAMPLE
If fname=doSomething then use:
echo '<input type="button" onclick="' . $_POST['fname'] . '()" />';
and your rendered HTML would be:
<input type="button" onclick="doSomething()" />
Then you'd just need your function somewhere:
<script>
function doSomething() { }
</script>
If your current page is welcome.php and you ran this code, you would press the Submit button and the page would reload and have your new button.
But the best way to see whether this works for you or not is to run it. Why don't you try that first?