php form name variable - 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.

Related

Why there's no output generated?

What wrong with my code? uniqid() is for generating a unique code and it will be stored to the name attribute then a condition is stated if it is clicked it will generate it's working.. Could some help me please with this one? Thanks in advance..
<html>
<form method="POST">
<?php
$attname = uniqid();
echo "<input type='submit' name='$attname' class='btn btn-success' value='Post to PeĆ³nline'/>";
echo $attname;
if(isset($_POST[$attname])){
echo 'its working';
}
?>
</form>
<html>
This isn't going to work.
When you refresh the page the $attname value will change. This will happen when you submit the form. So the actual name you're checking on will change and not be the same as the new $attname.
Put the following after your echo $attname; line:
print_r($_POST);
Also for this to work properly you'll need to nest the <input> tag in a <form> tag e.g.:
<form method="POST">
<input>...</input>
</form>
The $attname will change after page refresh.
You need to store that name somewhere.
Add another hidden element.
...
echo "<input type='hidden' name='elemname' value='<?php echo $attname;?>'/>";
...
And after submit,
if (isset($_POST['elemname'])) {
$elemname = $_POST['elemname'];
$val = isset($_POST[$elemname]) ? $_POST[$elemname] : '';
echo $val;
}

Getting the value of a button from a php script and posting the value to MySQL database

I have been working on this for days, I've Google'd and re-searched but I can't seem to find exactly what am looking for. I have a menu tab which is populated by a php script as follows:
<?php include 'db_connector.php';
$result = mysqli_query($con,"SELECT * FROM os_scope");
while($row = mysqli_fetch_array($result)) {
echo "<button type='radio'>"."<li>"."<div class='title'>"."<a href='#myPanel' >".$row['name']."</a>"."</div>"."</li>"."</button>";
}
?>
The output of the result is this:
Billing
Custom
Customer
Product
When I select any one of them i.e Billing it opens the #mypanel
I would like for each time I select an item i.e Billing, the value "Billing" should be posted to the panel, where I can then use this value to query MySQL database. And the same for the remaining items.
Any advice would be greatly appreciated. Thank you in advance.
There is no such thing as <button type='radio'>
Use <input type='radio' value='value'>
Try this:
echo "<li><input type='radio' value='".$row['name']."' /><div class='title'><a href='#myPanel' >".$row['name']."</a></div></li>";
Also, there is no reason to do this all over the place: "." Just let the string be a string. That's what strings do best.
This is exactly what I was looking for,
<!DOCTYPE html>
<html>
<body>
<?php include 'db_connector.php';
$result = mysqli_query($con,"SELECT * FROM os_scope");
while($row = mysqli_fetch_array($result)) {
$row['name'];
echo "<input type='radio' name='os' value=".$row['name']." id='myRadio'>";
}
?>
<p>Click the "Try it" button to display the value of the radio button.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementsByName('os');
var rate_value;
for(var i = 0; i < x.length; i++){
if(x[i].checked){
rate_value = x[i].value;
break;
}
}
document.getElementById("demo").innerHTML = rate_value;
}
</script>
</body>
</html>

Running javascript inside php

I have this javascript that i want to run in php,
the code bellow is supposed to be submitted in a form and then then prints it
but when submitted the javascript doesn't execute, the output is simply
var text = document.getElementById(\'course1\').options[document.getElementById(\'course1\').selectedIndex].text; document.write(text);
this is the whole thing,
echo "<form name\"find\" action=\"postEnrolled.php\" method=\"get\" class=\"required\" onsubmit=\"return validate(this);\">";
echo "<table width=\"225\" border=\"0\" align=\"center\" >";
echo "<tr>";
echo "<td width=\"181\"><label>Course#1:</label> <select name=\"dep1\" style=\"width:190px\" class=\"dep1\">";
echo "<option selected=\"selected\" value=\"\">Select Department</option>";
include('db.php');
$sql=mysql_query("select id,data from data where weight='1'");
while($row=mysql_fetch_array($sql))
{
$id = $row['id'];
$data = $row['data'];
echo '<option value="'.$id.'">'.$data.'</option>';
}
echo "</select><br/></td>";
echo "<td width=\"267\">";
echo "<label> </label><select name=\"course1\" class=\"course1\" style=\"width:200px\">";
echo "<option selected=\"selected\" value=\"\">Select Course</option>";
echo "</select>";
echo "<input type=\"hidden\" name=\"course_1\" value=\"
<script language='javascript' >
var text = document.getElementById('course1').options[document.getElementById('course1').selectedIndex].text;
document.write(text);
</script>
Am I missing something?
what I really want is to submit the text in the options and not the value of the options.
getElementById will only find an element whose id is course_1, not the name.
Don't put the script element inside the input element
You must have the DOM ready when calling it (use document.onload=function(){...yourcodehere...};)
At first sight, there is no PHP really involved in this problem. But are you aware that the code, as it is, wouldn't be executed when you change the value of the option ? If that's what you need, use onchange="yourcodehere;". But as it is an hidden field, maybe you should describe what you really want to achieve.
EDIT :
If what you want is change the hidden input when the user selects another option, here's how you can do it :
<input type=hidden name=course_1 id=course_1>
<select onchange="document.getElementById('course_1').value=this.options[this.selectedIndex].text;" ...
Your problem is that you're putting a <script> tag inside of the value attribute of the <input> tag. That isn't valid HTML or JavaScript and will not work.
why you don't post what is the actual error you got? actually this approach (including the javascript code into php tags) is not good at all.if you want to use javascript on any page, you just have to put it on the very top of your page under the script tags.
try it, will help u ..!

Add variable into (specify text field) on the previous page

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.

how to send a text box value without submitting form in php?

I want to refresh the same page and display the entered value in text box on the same page after clicking the link or button.
I have the following code:
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'>";
echo " <input type='text' name='myTextField'>";
echo "<a href='{$_SERVER['PHP_SELF']}?student_no=myTextField'> Search Student</a>";
if (!(isset($_GET[student_no])))
{
echo "No Student is available.";
}
else
{
$student_no = $_GET['student_no'];
echo "Student NO:".$studen_no;
}
?>
Please guide me how to achieve the goal and whats the error my code.
<?php
echo '<h3>Fee Payment</h3>';
if(isset($_POST['myTextField']))
$value=$_POST['myTextField'];
else
$value='';
echo "<form id='myFormId' name='myFormName' method='post'>";
echo " <input type='text' name='myTextField' value='$value'>";
echo "<a href='{$_SERVER['PHP_SELF']}?student_no=myTextField'> Search Student</a>";
You also need a submit button in the form, and a form close tag.
What you want is AJAX. I am just providing you an example of how you can proceed by using jQuery, but you can use any other library / framework or any other way also. You can check this article also for more usability details.
In the main page:-
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'>";
echo "<input type='text' name='myTextField' id='myTextField' />";
echo 'Search Student';
echo "</form>";
?>
<script type="text/javascript">
$('#inline_submit_a').click(function(evt){
$.ajax({
type: "GET",
url: "handler.php",
data: {text:$('#myTextField').val()}
});
evt.preventDefault();
return false;
});
</script>
In the "handler.php" page:-
<?php
if (!(isset($_GET[student_no])))
{
echo "No Student is available.";
}
else
{
$student_no = $_GET['student_no'];
echo "Student NO:".$student_no;
}
?>
You can write all the logic related to the database in the "handler.php" page.
Hope it helps.
You're using a link to submit the form, but you should be using a submit button.
<input type="submit" value="Search student" />
You didn't close the <form> tag.
You're using $_GET[student_no] which will make PHP look for a definition of student_no. Since it's a string, express it as one. $_GET['student_no'] is better.
Why can't you submit the form? Do you want to have a link for searching instead of a button?
You could configure the link to submit the form via javascript and change the form action to GET like this:
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'
action='{$_SERVER['PHP_SELF']}' method='GET'>";
echo " <input type='text' name='student_no'>";
echo "<a href='javscript:document.forms.myFormName.submit()'>
Search Student</a>";
...

Categories